【发布时间】:2012-01-07 10:20:37
【问题描述】:
我正在尝试将 xts 对象中包含的每日数据时间序列(仅工作日)转换为每周数据的时间序列。具体来说,我希望生成的时间序列包含原始数据的周末条目(即一周的最后一个工作日)。我一直在尝试使用 xts 包的 to.weekly 函数来实现这一点。
在关于另一个问题 (Wrong week-ending date using 'to.weekly' function in 'xts' package) 的讨论中,下面的示例代码完全符合我的需要。但是,当我运行代码时,to.weekly 使用 Mondays 作为每周数据的代表。
我想知道哪种全局设置可以让我强制 to.weekly 使用 Friday 作为一周的代表。
示例代码:
library(lubridate); library(xts)
test.dates <- seq(as.Date("2000-01-01"),as.Date("2011-10-01"),by='days')
test.dates <- test.dates[wday(test.dates)!=1 & wday(test.dates)!=7] #Remove weekends
test.data <- rnorm(length(test.dates),mean=1,sd=2)
test.xts <- xts(x=test.data,order.by=test.dates)
test.weekly <- to.weekly(test.xts)
test.weekly[wday(test.weekly, label = TRUE, abbr = TRUE) != "Fri"]
【问题讨论】:
标签: r time-series xts group-by