在寻找修改chron包中的.Holiday对象的方法时,我发现了这个解决方案 How to define holidays for is.holiday() chron package in R
本身效果非常好,除非我在hlist中包含“GBNewYearsEve”,我收到错误:
Error in get(as.character(FUN), mode = "function", envir = envir) :
object 'GBNewYearsEve' of mode 'function' was not found
如果从列表中删除GBNewYearsEve,则不会出现此错误。我错过了什么?
示例工作代码:
library(chron)
library(timeDate)
hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay")
(ss <- dates(sapply(sapply(hlist,holiday,year=(c(2011)),as.Date)))
.Holidays <- ss
chron::.Holidays ##nochange
unlockBinding(".Holidays", as.environment("package:chron"))
assignInNamespace(".Holidays", .Holidays, ns="chron",
envir=as.environment("package:chron"))
assign(".Holidays", .Holidays, as.environment("package:chron"))
lockBinding(".Holidays", as.environment("package:chron"))
chron::.Holidays ##change
非工作代码示例:
hlist <- c("GBMayDay", "GBBankHoliday", "GBSummerBankHoliday", "ChristmasEve", "ChristmasDay", "BoxingDay", "NewYearsDay", "GBNewYearsEve")
(ss <- dates(sapply(sapply(hlist,holiday,year=2011),as.Date)))
答案 0 :(得分:2)
不确定这是否适合您。我很好奇你的问题,我已经从CRAN下载了timeDate
软件包。虽然似乎在?holiday
中有记录,但我认为代码已为GBNewYearsEve
做好准备。
如果我按原样运行你的代码:
> 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
(很抱歉混合语言,基本上错误信息是说找不到GBNewYearsEve
。我实际上没有在timeDate
的代码中找到它。但是,如果我添加如下定义:
GBNewYearsEve =
function(year = getRmetricsOptions("currentYear")) {
ans = year*10000 + 1231
timeDate(as.character(ans)) }
(这基本上是从DENewYearsEve
复制的,这是新年的唯一定义,包中有夏娃)
然后我运行你的代码:
> (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
但是我不确定这个解决方案有多好。请注意,在dateTime
中,进行了一些额外的转换,例如当假期在一个周末,它被移动到第二天。通过上面的代码,您将获得新的一年&#39;夏娃在12月31日。
例如,这是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))
}
我想这个套餐只处理每个国家/地区的官方假期,并添加这些附加规则?