I've checked a few questions on time series aggregation, but haven't found what I was looking for.
I have a zoo
object of weekly data over 10 years, and I've calculated the monthly rolling mean of this series. I want to work with monthly data that corresponds to this, and to that end pull the last weekly data point for each month, as representative for the whole month, into a separate time series.
I've tried the aggregate
ts function but am having trouble since the spacing is not even (ie 4 weeks not always a month, etc).
The fact that months have a different length should be no problem. Look at the following example:
> library(xts)
> dates <- as.Date("2013-01-01") + 7*(0:52)
> y.week <- zoo(rnorm(365),order.by=dates)
> head(y.week)
2013-01-01 2013-01-08 2013-01-15 2013-01-22 2013-01-29 2013-02-05
0.3648757 -0.7247121 0.4815929 0.5967042 1.6272563 0.8472303
> y.mon <- aggregate(y.week,
+ by = format(dates,format="%Y-%m"),
+ FUN=sum)
> head(y.mon)
2013-01 2013-02 2013-03 2013-04 2013-05 2013-06
2.3457170 1.9640368 -0.3259688 -0.8710992 0.5430847 -0.1107915
Look at the help page for strptime
to see the format strings you can use in place of "%Y-%m"
above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.