3

我有一个问题,其中的endpoints()函数xts(以及to.weekly使用端点的函数)有时返回星期五作为周末,有时返回星期一。

我的数据集称为sp2.

> head(sp2)
           [,1]
2012-01-09 1.78
2012-01-10 1.78
2012-01-11 1.77
2012-01-12 1.80
2012-01-13 1.77
2012-01-16 1.77

> tail(sp2)
           [,1]
2012-06-28 1.94
2012-06-29 1.92
2012-07-02 1.92
2012-07-03 1.90
2012-07-04 1.89
2012-07-05 1.89

> class(sp2)
[1] "xts" "zoo"

> weekdays(index(sp2))
  [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [11] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [21] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [31] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [41] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [51] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [61] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [71] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [81] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
 [91] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[101] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[111] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"   
[121] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Monday"    "Tuesday"   "Wednesday" "Thursday" 

如您所见,数据系列中没有缺少工作日。

然而.....

> weekdays(index(to.weekly(sp2)))
 [1] "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Friday"   "Monday"  
[12] "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"  
[23] "Monday"   "Monday"   "Monday"   "Thursday"

当然这应该总是星期五(除了今天,星期四的最后一点)?

我在这里做错了什么,有解决方法吗?

我希望计算这个数据系列从周五到周五的每周变化。

......................................可重复的例子...... .....................

i <- as.Date((as.numeric(Sys.Date())-200):as.numeric(Sys.Date()))
x <- xts(rep(1, length(i)), order.by = i)
weekdays(index(to.weekly(x)))
#  [1] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [14] "Sunday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"   "Monday"  
# [27] "Monday"   "Monday"   "Monday"   "Thursday"
y <- xts(rep(1, length(i)), order.by = as.POSIXct(i))
weekdays(index(to.weekly(y)))
# [1] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [14] "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"   "Sunday"  
# [27] "Sunday"   "Sunday"   "Sunday"   "Thursday"
4

1 回答 1

3

这是 CRAN 版本中的一个错误,已在 R-Forge 上修复。基本上,索引的时间部分并没有被删除,并且您遇到了一些带有时区和夏令时的时髦业务。

从 R-Forge 安装以使用更正的功能。

require(xts)
i <- Sys.Date()+200:0
x <- xts(rep(1, length(i)), order.by = i)
y <- xts(rep(1, length(i)), order.by = as.POSIXct(i))
all.equal(weekdays(index(to.weekly(x))), weekdays(index(to.weekly(y))))
于 2012-07-05T15:54:39.837 回答