【问题标题】:lubridate - select first non-Monday of every week.lubridate - 选择每周的第一个非星期一。
【发布时间】:2017-01-11 08:12:21
【问题描述】:

有一些财务数据,我想通过只选择每周的第一个非星期一来过滤它。通常是星期二,但如果星期二是假期,有时可能是星期三。

这是我在大多数情况下都有效的代码

XLF <- quantmod::getSymbols("XLF", from = "2000-01-01", auto.assign = FALSE)

library(tibble)
library(lubridate)
library(dplyr)
xlf <- as_tibble(XLF) %>% rownames_to_column(var = "date") %>% 
         select(date, XLF.Adjusted)  
xlf$date <- ymd(xlf$date)

# We create Month, Week number and Days of the week columns
# Then we remove all the Mondays
xlf <- xlf %>% mutate(Year = year(date), Month = month(date), 
                      IsoWeek = isoweek(date), WDay = wday(date)) %>% 
               filter(WDay != 2)

# Creating another tibble just for ease of comparison
xlf2 <- xlf %>% 
          group_by(Year, IsoWeek) %>% 
          filter(row_number() == 1) %>% 
          ungroup()

也就是说,到目前为止,我还无法解决一些问题。

问题在于它跳过了星期二的“2002-12-31”,因为它被视为 2003 年第一周 ISO 的一部分。 有几个类似的问题。
我的问题是,我如何在没有此类问题的情况下选择每周的第一个非星期一,同时留在 tidyverse(即不必使用 xts / zoo 类)?

【问题讨论】:

    标签: r dplyr lubridate tidyverse


    【解决方案1】:

    您可以自己创建一个持续增加的周数。也许不是最优雅的解决方案,但对我来说效果很好。

    as_tibble(XLF) %>% 
      rownames_to_column(var = "date")%>% 
      select(date, XLF.Adjusted)%>%
      mutate(date = ymd(date),
             Year = year(date),
             Month = month(date),
             WDay = wday(date),
             WDay_label = wday(date, label = T))%>% 
      # if the weekday number is higher in the line above or 
      # if the date in the previous line is more than 6 days ago
      # the week number should be incremented
      mutate(week_increment  = (WDay < lag(WDay) | difftime(date, lag(date), unit = 'days') > 6))%>%
      # the previous line causes the first element to be NA due to 
      # the fact that the lag function can't find a line above
      # we correct this here by setting the first element to TRUE
      mutate(week_increment = ifelse(row_number() == 1,
                                     TRUE,
                                     week_increment))%>%
      # we can sum the boolean elements in a cumulative way to get a week number
      mutate(week_number = cumsum(week_increment))%>%
      filter(WDay != 2)%>%
      group_by(Year, week_number) %>% 
      filter(row_number() == 1)
    

    【讨论】:

    • 相当迂回,但它完全可以完成工作。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2021-08-21
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多