3
我有一個問題,xts
中的endpoints()
函數(以及使用端點的to.weekly
函數)有時會在週末結束時返回星期五,有時會返回星期一。XTS to.weekly returns each different endpoints
我的數據集名爲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"
當然這應該是週五(除了最後一點也就是今天,週四)?
我在做什麼錯在這裏,是否有解決方法?
我希望計算從週五到週五這個數據系列的每週更改。
.......................................... REPRODUCIBLE EXAMPLE .. ......................
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"
你使用的是什麼版本的xts? – 2012-07-05 15:07:57
我剛升級到R2.15.1並運行update.packages()。顯示「0.8-2」。 – 2012-07-05 15:13:06
糟糕 - 我看到我們在0.8-6。 更新(年/月/日)? y 警告:包'xts','zoo'正在使用中,不會安裝 這可能是問題所在。讓我修復它.... – 2012-07-05 15:15:30