使用 apply.weekly() 强制整周 [英] Forcing full weeks with apply.weekly()

查看:17
本文介绍了使用 apply.weekly() 强制整周的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚在执行 apply.period 之后 xts(或 zoo)使用什么作为时间.考虑以下几点:

I'm trying to figure out what xts (or zoo) uses as the time after doing an apply.period. Consider the following:

> myTs = xts(1:10, as.Date(1:10, origin = '2012-12-1'))
> apply.weekly(myTs, colSums)
           [,1]
2012-12-02    1
2012-12-09   35
2012-12-11   19

我认为2012-12-02"的意思是截至 2012-12-02 的那一周,总和为 1".所以基本上时间是周末.

I think the '2012-12-02' means "for the week ending 2012-12-02, the sum is 1". So basically the time is the end of the week.

但问题在于2012-12-11" - 我认为它正在做的是说 11 号是一周的最后一天 那个给出,所以它给了时间.

But the problem is with that "2012-12-11" - I think what it's doing is saying that the 11th is the last day of the week that was given, so it's giving that as the time.

有没有办法强制它给出结束的星期日,即使那一天没有包含在数据集中?

Is there any way to force it to give the sunday on which it ends, even if that day was not included in the data set?

推荐答案

试试这个:

nextsun <- function(x) 7 * ceiling(as.numeric(x-0+4) / 7) + as.Date(0-4)
aggregate(myTs, nextsun, sum)

其中 nextsun 源自 动物园快速参考,将 5(星期五)替换为 0(星期日).

where nextsun was derived from nextfri code given in the zoo quick reference by replacing 5 (for Friday) with 0 (for Sunday).

这篇关于使用 apply.weekly() 强制整周的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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