[英]R: splitting long zoo time series into calendar
我有一個長達數年的動物園對象。
這是每日報價的時間序列(第一列:日期,第二列:報價)
我希望將這個長期序列划分為日歷年子集,最終目標是將這些數據繪制在一張圖表中,其水平軸為一年。
(我不想將每日的原始數據轉換成每月的數據或任何其他時間步長...)。
謝謝。
這是一種方法
library(quantmod)
getSymbols("SPY", src='yahoo', from='2000-01-01', to='2012-01-01')
SPY <- as.zoo(Ad(SPY))
# Now I have zoo data with a single value for each day (like you say you have)
# Convert it to xts so that I can use xts' split method
mydata <- as.xts(SPY)
# Split data by years. Give them rownames that are day of year instead of
# specific dates. cbind data together and plot
ts.plot(do.call(cbind, lapply(split(mydata, 'years'), function(x) {
zoo(x, 1:NROW(x))
})), col=rainbow(nyears(mydata)))
或者,
tapply(SPY, format(index(SPY), "%Y"), c)
將按年份拆分,不需要轉換為xts
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.