2
votes

I have a raster time series created as a zoo onbect. I am able to aggregate quarterly means but I want the aggregates from December-February,March-May,June-August and September to Novermber. How can I make this possible please? Normal R code is:

library(raster)
library(zoo)
C <- raster(nrows=30, ncols=30)
C[] <- 1:ncell(C)
O <- raster(nrows=30, ncols=30)
O[] <- 1:ncell(O)
CO1 <- stack(C,C,C,C,O,O,O)
m <- seq(as.Date('2009-12-15'), as.Date('2010-06-15'), 'month')
CO2 <- setZ(CO1,m)
CN <- zApply(CO2, by=as.yearqtr, fun=mean)

This would yield a quarterly mean for December,January-March and April-June. What I want is a mean for Dec-Feb, Mar-May,June.

2
Example code should be reproducible which means that all inputs needed to run it should be provided.G. Grothendieck
I have expanded the code a bitJoke O.
To be reproducible it also needs all library statements.G. Grothendieck

2 Answers

3
votes

Try:

zApply(CO2, by=round(unclass(getZ(CO2)) * 12 + 1) %/%3, fun=mean)
0
votes

You can use stackApply, which is conceptually similar to tapply. Not as nifty as zApply because it can be more laborious to set up the indices you need.

stackApply(CO2, c(1,1,1,2,2,2,3), fun=mean)