问题描述
限时送ChatGPT账号..我查了几个关于时间序列聚合的问题,但没有找到我想要的.
I've checked a few questions on time series aggregation, but haven't found what I was looking for.
我有一个zoo
对象,包含超过 10 年的每周数据,并且我计算了这个系列的月滚动平均值.我想使用与此相对应的月度数据,并为此将每个月的最后一个每周数据点(作为整个月的代表)提取到一个单独的时间序列中.
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.
我已经尝试了 aggregate
ts 函数,但由于间距不均匀(即 4 周,并非总是一个月等),因此遇到了问题.
I've tried the aggregate
ts function but am having trouble since the spacing is not even (i.e. 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
查看 strptime
的帮助页面以查看您可以使用的格式字符串代替上面的 "%Y-%m"
.
Look at the help page for strptime
to see the format strings you can use
in place of "%Y-%m"
above.
这篇关于从每周数据中获取每月时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论