Reputation: 11849
I want to sum the values in each column of a frame by week. I can do the mean, but the sum doesn't work for some reason:
> zoo.data <- zoo(data.frame(x=11:20,y=1:10),as.Date(1:10,origin="1970-01-01"))
> apply.weekly(zoo.data, mean)
x y
1970-01-04 12 2
1970-01-11 17 7
> apply.weekly(zoo.data, sum)
1970-01-04 1970-01-11
42 168
What's going on?
Upvotes: 5
Views: 2029
Reputation: 49810
This is a result of the fact that the xts authors have decided to add a mean.xts
method to mimic the old behavior of base R (and which is essentially colMeans
). mean.xts
is now dispatched on xts
objects instead of mean.default
, and apply.weekly
temporarily converts your zoo
object to an xts
internally.
R> apply.weekly(zoo.data, mean)
x y
1970-01-04 12 2
1970-01-11 17 7
R> apply.weekly(zoo.data, mean.default)
1970-01-04 1970-01-11
7 12
But, I think this is what you want to do:
R> apply.weekly(zoo.data, colMeans)
x y
1970-01-04 12 2
1970-01-11 17 7
R> apply.weekly(zoo.data, colSums)
x y
1970-01-04 36 6
1970-01-11 119 49
Upvotes: 8