例如,如果是12月1日,我希望它退出Wed,11月25日到12月1日星期二(包括这两天)。
注意:last()不是答案,因为它获取xts对象中的最新数据,而不考虑当前日期。
x = xts(1:10, as.Date("2015-12-01") - (30:21))
last(x,"1 week") #2015-11-09, 2015-11-10
last(x,"7 days") #2015-11-04 to 2015-11-10 inclusive.在本例中,我要查找的函数将返回一个空对象。
理想情况下,该解决方案将拥有last()函数的所有功能,因为能够编写last(x,"2 hours")、last(x,"6 months")等非常棒。
发布于 2015-12-01 19:02:56
这不应该像下面这样简单:
library(xts)
endDate = as.Date("2015-12-01")
startDate = endDate - 6
x = xts(1:100, as.Date("2015-12-30") - (1:100))
x[index(x) >= startDate & index(x) <= endDate,]
# [,1]
# 2015-11-25 35
# 2015-11-26 34
# 2015-11-27 33
# 2015-11-28 32
# 2015-11-29 31
# 2015-11-30 30
# 2015-12-01 29
x = xts(1:100, as.Date("2015-11-15") - (1:100))
x[index(x) >= startDate & index(x) <= endDate,] # Nothing there https://stackoverflow.com/questions/34018495
复制相似问题