【发布时间】: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