从 R 中的时间日期获取借出
Get Lent from timeDate in R
我想获取从 2010 年到 2020 年的所有四旬期星期五。我目前正在使用 timeDate
获取复活节、耶稣受难日和圣灰星期三等假期。如下
aw <- as.Date(AshWednesday(year = 2010:2020))
gf <- as.Date(GoodFriday(year = 2010:2020))
我也可以获得套餐中没有的固定假期。例如
mg <- as.Date(AshWednesday(year = 2010:2020)-1) #Mardi Gras
cm <- as.Date(seq(ymd('2010-05-05'),ymd('2020-05-05'), by = '1 year')) #cinco de mayo
但我正在努力获得每年所有的四旬期星期五。
注意:四旬期从灰烬星期三之后的星期日开始,持续 40 天。
如下所示,以下代码有效:
library(dplyr)
library(purrr)
library(tidyr)
library(lubridate)
tibble(aw) %>%
mutate(aw_sunday = aw + lubridate::days(0)) %>% #Find the first Sunday after each Ash Wednesday
mutate(extra_days = map(aw_sunday, function(x) x + lubridate::days(1:40))) %>% #Find all series of 40 days after each sunday
unnest %>%
mutate(week_day = lubridate::wday(extra_days, label = TRUE)) %>% #Find all the day names
filter(week_day == 'Fri') %>% # Filter out the fridays
pull(extra_days)
我将 days(4)
更改为 days(0)
因为我认为借出的星期五实际上是在 Ash 星期三之后立即开始的,而不是跳过一周。我从 "the Ambrosian Rite" 下的维基百科中提取了上面的注释。我猜是有区别的。
如果四旬期在灰烬星期三之后的星期日开始,那么所有随后的星期五都是灰烬星期三之后的第 9、16、...、44 天。您可以使用 lubridate 中的 days
或 ddays
函数为各个日期添加持续时间。由于您有日期向量,我们可以将四旬期星期五存储在列表中。
library(lubridate)
lent_fridays <- lapply(aw, function(x) x + days(seq(9, 44, by =7)))
例如,对于 2018 年,四旬期星期五是以下日期。
[[9]]
[1] "2018-02-23" "2018-03-02" "2018-03-09" "2018-03-16" "2018-03-23"
[6] "2018-03-30"
这是一种方法:
library(dplyr)
library(purrr)
library(tidyr)
library(lubridate)
tibble(aw) %>%
mutate(aw_sunday = aw + lubridate::days(4)) %>% #Find the first Sunday after each Ash Wednesday
mutate(extra_days = map(aw_sunday, function(x) x + lubridate::days(1:40))) %>% #Find all series of 40 days after each sunday
unnest %>%
mutate(week_day = lubridate::wday(extra_days, label = TRUE)) %>% #Find all the day names
filter(week_day == 'Fri') %>% # Filter out the fridays
pull(extra_days)
如果不想使用 lubridate,可以使用基础 seq.Date 函数:
gf <- as.Date(GoodFriday(year = 2010:2020))
lentFridays <- lapply(gf, function(x)seq.Date(x, length.out = 6, by = "-7 days"))
美化它:
lentFridays <- data.frame(lentFridays)
names(lentFridays) <- paste0("Year", 2010:2020)
我想获取从 2010 年到 2020 年的所有四旬期星期五。我目前正在使用 timeDate
获取复活节、耶稣受难日和圣灰星期三等假期。如下
aw <- as.Date(AshWednesday(year = 2010:2020))
gf <- as.Date(GoodFriday(year = 2010:2020))
我也可以获得套餐中没有的固定假期。例如
mg <- as.Date(AshWednesday(year = 2010:2020)-1) #Mardi Gras
cm <- as.Date(seq(ymd('2010-05-05'),ymd('2020-05-05'), by = '1 year')) #cinco de mayo
但我正在努力获得每年所有的四旬期星期五。
注意:四旬期从灰烬星期三之后的星期日开始,持续 40 天。
如下所示,以下代码有效:
library(dplyr)
library(purrr)
library(tidyr)
library(lubridate)
tibble(aw) %>%
mutate(aw_sunday = aw + lubridate::days(0)) %>% #Find the first Sunday after each Ash Wednesday
mutate(extra_days = map(aw_sunday, function(x) x + lubridate::days(1:40))) %>% #Find all series of 40 days after each sunday
unnest %>%
mutate(week_day = lubridate::wday(extra_days, label = TRUE)) %>% #Find all the day names
filter(week_day == 'Fri') %>% # Filter out the fridays
pull(extra_days)
我将 days(4)
更改为 days(0)
因为我认为借出的星期五实际上是在 Ash 星期三之后立即开始的,而不是跳过一周。我从 "the Ambrosian Rite" 下的维基百科中提取了上面的注释。我猜是有区别的。
如果四旬期在灰烬星期三之后的星期日开始,那么所有随后的星期五都是灰烬星期三之后的第 9、16、...、44 天。您可以使用 lubridate 中的 days
或 ddays
函数为各个日期添加持续时间。由于您有日期向量,我们可以将四旬期星期五存储在列表中。
library(lubridate)
lent_fridays <- lapply(aw, function(x) x + days(seq(9, 44, by =7)))
例如,对于 2018 年,四旬期星期五是以下日期。
[[9]]
[1] "2018-02-23" "2018-03-02" "2018-03-09" "2018-03-16" "2018-03-23"
[6] "2018-03-30"
这是一种方法:
library(dplyr)
library(purrr)
library(tidyr)
library(lubridate)
tibble(aw) %>%
mutate(aw_sunday = aw + lubridate::days(4)) %>% #Find the first Sunday after each Ash Wednesday
mutate(extra_days = map(aw_sunday, function(x) x + lubridate::days(1:40))) %>% #Find all series of 40 days after each sunday
unnest %>%
mutate(week_day = lubridate::wday(extra_days, label = TRUE)) %>% #Find all the day names
filter(week_day == 'Fri') %>% # Filter out the fridays
pull(extra_days)
如果不想使用 lubridate,可以使用基础 seq.Date 函数:
gf <- as.Date(GoodFriday(year = 2010:2020))
lentFridays <- lapply(gf, function(x)seq.Date(x, length.out = 6, by = "-7 days"))
美化它:
lentFridays <- data.frame(lentFridays)
names(lentFridays) <- paste0("Year", 2010:2020)