gpt4 book ai didi

r - XTS to.weekly 返回不同的每周端点

转载 作者:行者123 更新时间:2023-12-04 09:11:57 25 4
gpt4 key购买 nike

我有时会遇到 xts 中的 endpoints() 函数(以及使用端点的 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"

最佳答案

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

Install from 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))))

关于r - XTS to.weekly 返回不同的每周端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11347081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com