使用 MyCalendar 将日期列偏移前一个工作日

问题描述 投票:0回答:0

我有一个这种结构的数据框:

structure(list(Date = structure(c(19377, 19419, 19390, 19410, 
19425, 19397, 19411, 19368, 19403, 19431, 19375, 19424, 19418, 
19382, 19438, 19363, 19396, 19439), class = "Date"), Value = c(1195.925, 
736.825, 3000, 3500, 2500, 2000, 2000, 3702.5, 3000, 3749.999, 
4440.75, 4500, 3124.999, 6000, 2464.999, 3500, 3500, 3500)), row.names = c(NA, 
-18L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x000002c1839a1ef0>)

我想根据预定义的日历将日期列的内容更改为前一个工作日:

create.calendar(name = "MyCalendar", holidays = holidayLONDON(1980:getRmetricsOption("currentYear")), weekdays = c("saturday", "sunday"), financial = TRUE)
df$Date <- df[df$Date == offset(Date, -1, "MyCalendar")]

这段代码通常会给出一个没有任何错误的好结果,但是 不幸的是,在这种情况下运行代码时出现以下错误:

Error in set(x, j = name, value = value) : 
  Supplied 13 items to be assigned to 1463 items of column 'Date'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.

这对我来说似乎很奇怪,因为我相信我给出的日历非常大。我该如何解决这个问题?

r lubridate
© www.soinside.com 2019 - 2024. All rights reserved.