This is really an extension of my question yesterday, where I found out about apply.weekly. This works great, but I want to do this on wide objects zoo. If I use apply.weeklywide zoo, it sums up the columns, then performs weekly aggregation:
> library(xts)
> set.seed(2001)
> zoo.daily <- zoo(data.frame(a=rnorm(20), b=rnorm(20), c=rnorm(20)), order.by=as.Date("2001-05-25") + 0:19)
> apply.weekly(zoo.daily, sum)
2001-05-27 2001-06-03 2001-06-10 2001-06-13
1.091999 -3.017688 3.842305 2.045370
> apply.weekly(zoo.daily[, 1] + zoo.daily[, 2] + zoo.daily[, 3], sum)
2001-05-27 2001-06-03 2001-06-10 2001-06-13
1.091999 -3.017688 3.842305 2.045370
I tried a family of statements apply, but they seem to allocate a date index zoo. I can do this in a loop for, but it really takes a lot of time (much, much more than four times slower than a function aggregateon periodicity as.yearmon). Here's the loop for:
week.ends <- index(zoo.daily[endpoints(zoo.daily, "weeks")[-1], ])
num.weeks <- nweeks(zoo.daily)
num.stocks <- ncol(zoo.daily)
zoo.weeks <- zoo(matrix(NA, num.weeks, num.stocks), order.by=week.ends)
for (i in seq(num.stocks)) {
zoo.weeks[, i] <- apply.weekly(zoo.daily[, i], mean)
}
What works (i.e. saves each vector separately):
2001-05-27 -0.36663040 -0.108648725 0.8392788
2001-06-03 0.33032998 0.003025018 -0.7644534
2001-06-10 0.07816992 0.620198931 -0.1494681
2001-06-13 0.02114608 0.956226189 -0.2955824
apply.weekly? !
: , (, colMeans colSums). , , . ? / ?
> apply.weekly(zoo.daily, colSums)
[,1] [,2] [,3] [,4]
a -1.0998912 2.31230989 0.5471894 0.06343824
b -0.3259462 0.02117512 4.3413925 2.86867857
c 2.5178365 -5.35117351 -1.0462765 -0.88674717