简体   繁体   English

从R中的timeDate获取借出

[英]Get Lent from timeDate in R

I would like to get all Lent Fridays from 2010 to 2020. I am currently using timeDate to get holidays such as Easter, Good Friday, and Ash Wednesday. 我希望获得从2010年到2020年的所有四旬期星期五。我目前正在使用timeDate度假,例如复活节,耶稣受难日和Ash Ash周三。 As follows 如下

aw <- as.Date(AshWednesday(year = 2010:2020))
gf <- as.Date(GoodFriday(year = 2010:2020))

I can also get fixed holidays that don't come with the package. 我还可以得到固定的假期,而假期不包含在套餐中。 For example 例如

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

But I am struggling to get all Lent Fridays per year. 但是我每年都在努力获得所有四旬斋。

Note: Lent begins on the Sunday that follows Ash Wednesday and lasts for 40 days. 注意:四旬斋在灰烬星期三之后的星期日开始,持续40天。

As posted below, the following code worked: 如下所示,以下代码有效:

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)

I changed days(4) to days(0) because I think lent Fridays actually start immediately after Ash Wednesday, not skipping a week. 我将days(4)更改为days(0)因为我认为借出的周五实际上是在Ash Ash周三之后立即开始,而不是跳过一周。 I pulled the Note above from Wikipedia under "the Ambrosian Rite". 我从Wikipedia的“ Ambrosian Rite”下提取了上述注释。 I guess there is a difference. 我想是有区别的。

Here's one way to do it: 这是一种实现方法:

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)

If you don't want to use lubridate, you can use the base seq.Date function: 如果您不想使用lubridate,则可以使用基本seq.Date函数:

gf <- as.Date(GoodFriday(year = 2010:2020))
lentFridays <- lapply(gf, function(x)seq.Date(x, length.out = 6, by = "-7 days"))

and to pretty it up: 并使其漂亮:

lentFridays <- data.frame(lentFridays)
names(lentFridays) <- paste0("Year", 2010:2020)

If Lent begins on the Sunday after Ash Wednesday, then all subsequent Fridays are those that are 9, 16, ..., 44 days after Ash Wednesday. 如果四旬斋在灰烬星期三之后的星期日开始,那么随后的所有星期五都是灰烬星期三之后的9、16,...,44天。 You can use the days or ddays functions in lubridate to add time durations to individual dates. 您可以使用daysddays功能lubridate到持续时间增加个别日期。 Since you have a vector of dates, we can store the Lent Fridays in a list. 由于您具有日期向量,因此我们可以将四旬斋节存储在列表中。

library(lubridate)
lent_fridays <- lapply(aw, function(x) x + days(seq(9, 44, by =7)))

eg, for 2018, the Lent Fridays are the following dates. 例如,对于2018年,四旬期星期五是以下日期。

[[9]]
[1] "2018-02-23" "2018-03-02" "2018-03-09" "2018-03-16" "2018-03-23"
[6] "2018-03-30"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM