Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing full weeks with apply.weekly()

Tags:

r

zoo

xts

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

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.

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?

like image 725
Xodarap Avatar asked Dec 19 '12 02:12

Xodarap


1 Answers

Try this:

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

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

like image 59
G. Grothendieck Avatar answered Sep 27 '22 22:09

G. Grothendieck