In using timeDate R package, I receive an error when specifying GBNewYearsEve

流过昼夜 提交于 2019-12-06 06:15:50
lrnzcig

Not sure this is an answer that will suit you. I was curious with your problem and I've downloaded the timeDate package from CRAN. Although it seems to be documented in ?holiday, I don't think the code is ready for GBNewYearsEve.

If I run your code as it is I get:

> hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay", "GBNewYearsEve")
> 
> (ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
Error in get(as.character(FUN), mode = "function", envir = envir) : 
  el objeto 'GBNewYearsEve' de modo 'function' no fue encontrado

(Sorry for the mixture of languages, basically the error message is saying that GBNewYearsEve was not found. I actually don't find it in the code of timeDate. However, if I add a definition like this:

GBNewYearsEve =
  function(year = getRmetricsOptions("currentYear")) {
    ans = year*10000 + 1231
    timeDate(as.character(ans)) } 

(Which is basically copied from DENewYearsEve, the only definition for New Years' Eve present in the package)

Then I get your code running:

> (ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
           GBMayDay       GBBankHoliday GBSummerBankHoliday        ChristmasEve        ChristmasDay           BoxingDay 
           05/02/11            05/30/11            08/29/11            12/24/11            12/25/11            12/26/11 
        NewYearsDay       GBNewYearsEve 
           01/01/11            12/31/11 

However I'm not sure how good a solution is this. Note that in dateTime, some additional transformations are done so that e.g. when the holiday falls in a weekend it is moved to the following day. With the code above, you get just the New Years' Eve on the 31th of December.

For example, this is in holiday-LONDON.R:

        # New Year's Day: if it falls on Sat/Sun, then is
        # moved to following Monday
        posix1 <- as.POSIXlt(NewYearsDay(y))
        if (posix1$wday == 0 | posix1$wday == 6) {
            lon <- timeDate(.on.or.after(y, 1, 1, 1), zone = "London",
                            FinCenter = "Europe/London")
            holidays <- c(holidays, as.character(lon))
        } else {
            holidays <- c(holidays, as.character(posix1))
        }

I guess the package is handling only official holidays for each country, and adding those additional rules?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!