简体   繁体   English

在timeDate包中循环假期

[英]Loop over holidays in timeDate package

I would like to extract some holidays using library(timeDate) . 我想使用library(timeDate)提取一些假期。 I have used the following syntax first: 我首先使用以下语法:

EasterSunday<- as.Date(EasterSunday(2015:2018))

EasterSunday
# [1] "2015-04-05" "2016-03-27" "2017-04-16" "2018-04-01"

Then I wanted to add the corresponding holiday that date sequence: 然后我想添加相应的假期那个日期序列:

EasterSunday<- cbind.data.frame(hd=rep('EasterSunday',length(as.Date(EasterSunday(2015:2018)))),date=as.Date(EasterSunday(2015:2018)))

EasterSunday

        #hd            #date

#1 EasterSunday      2015-04-05
#2 EasterSunday      2016-03-27
#3 EasterSunday      2017-04-16
#4 EasterSunday      2018-04-01

Then I wanted to loop over all holidays in that package: 然后,我想遍历该包中的所有假期:

holidays=c("GoodFriday","EasterSunday","EasterMonday")

# Here I could not find the appropriate function
do.call(cbind, lapply(holidays, function(x) EasterSunday((2015:2018))))

#[,1] [,2] [,3]
#[1,] ?    ?    ?   

Using match.fun : 使用match.fun

library(timeDate)

holidays <- c("GoodFriday", "EasterSunday", "EasterMonday")

do.call(rbind,
        lapply(holidays, function(i){
          foo <- match.fun(i)  
          data.frame(Holiday = i,
                     Dates = as.Date(foo(2015:2018)))

        }))
#         Holiday      Dates
# 1    GoodFriday 2015-04-03
# 2    GoodFriday 2016-03-25
# 3    GoodFriday 2017-04-14
# 4    GoodFriday 2018-03-30
# 5  EasterSunday 2015-04-05
# 6  EasterSunday 2016-03-27
# 7  EasterSunday 2017-04-16
# 8  EasterSunday 2018-04-01
# 9  EasterMonday 2015-04-06
# 10 EasterMonday 2016-03-28
# 11 EasterMonday 2017-04-17
# 12 EasterMonday 2018-04-02

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

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