R:将动物园的长时间序列拆分为日历 [英] R: splitting long zoo time series into calendar

查看:29
本文介绍了R:将动物园的长时间序列拆分为日历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个长达数年的动物园对象.

I have a several-year length zoo object.

这是每日报价的时间序列(第一列:日期,第二列:报价)

This is a time series of daily quotes (first column : date, second column : quote)

我希望将这个长时间序列拆分为日历年子集,最终目标是将这些数据绘制在一个图表中,其水平轴为一年.

I wish to split this long time series into calendar year subsets, the ultimate goal being to plot those data in a single chart, the horizontal axis of which being one year-length.

(我不想将源日数据转换为月数据或任何其他时间步长...).

(I don't want to convert my source daily data into monthly ones or any other timestep ...).

谢谢.

推荐答案

这是一种方法

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

will split by year and does not require converting to xts

这篇关于R:将动物园的长时间序列拆分为日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆