[R-sig-Geo] zApply

Oscar Perpiñán Lamigueiro oscar.perpinan at gmail.com
Wed Apr 24 19:27:14 CEST 2013


Hello Kate,

I have modified your code. Try the code I copy below.

Best,

Oscar.

library(zoo)
## You were using library(base) but I am sure you really want raster :-)
library(raster)

## Data example: substitute it with your brick and time index
r <- raster(ncol=2, nrow=2)
index <- seq(as.Date('2000-01-01'), as.Date('2012-12-31'), by='day')
RR <- brick(lapply(seq_along(index), function(x) setValues(r, rnorm(ncell(r)))))

RR <- setZ(RR,index)
## If you only need monthly averages, first you have to define an
## auxiliary function to extract the month from the time index
month <- function(x)as.numeric(format(x, '%m'))
RRmonth <- zApply(RR, by=month, FUN=mean)
## But if you need monthly averages per year, zoo::as.yearmon is
## your function
RRyearmon <- zApply(RR,by=as.yearmon, FUN=mean)

KaTe <tersagokatrien at gmail.com> writes:

> Dear R-sig-geo, 
> I have a problem with the use of zApply on a large Rasterbrick made from a
> netcdf file. 
> It concerns a file with daily rainfall data for a period of 15 years on a
> european scale (see details below)
>
> -------------------------
> class       : RasterBrick 
> dimensions  : 201, 464, 93264, 6575  (nrow, ncol, ncell, nlayers)
> resolution  : 0.25, 0.25  (x, y)
> extent      : -40.5, 75.5, 25.25, 75.5  (xmin, xmax, ymin, ymax)
> coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
> names       : X1995.01.01, X1995.01.02, X1995.01.03, X1995.01.04,
> X1995.01.05, X1995.01.06, X1995.01.07, X1995.01.08, X1995.01.09,
> X1995.01.10, X1995.01.11, X1995.01.12, X1995.01.13, X1995.01.14,
> X1995.01.15, ... 
> Date        : 1995-01-01, 2012-12-31 (min, max)
> varname     : rr 
> --------------------------
>
> Now, I want to change this brick with daily data into a brick of monthly
> means (per year) prior to data extraction. I am quite convinced I should use
> zApply for this, but up till now I was not able to use (read: fully
> understand) the few examples from the raster manual or this list
> successfully. 
> I tried the code below, with RR being the raster brick: 
>
> library(zoo)
> library(base)
> index<- seq(as.Date('1995-01-01'), as.Date('2012-12-31'), by='day')
> RR<-setZ(RR,index)
> system.time(RRmonths<-zApply(RR,by="month", FUN=mean))
>
> when I try this on a subset of 2 years I only get a single raster file, so
> clearly something is wrong. 
> Even if this works, I fear this code will give me the monthly averages over
> the whole series, while I need mean monthly values per year (using yearmon
> gives errors). 
>
> I am new to this field, hope anyone could give me some hints to move
> forward. (Or some other approach )
> Thanks in advance,
>  
> Kate
> PhD, University of Antwerp
>
>
>
>
>
>
>
> --
> View this message in context: http://r-sig-geo.2731867.n2.nabble.com/zApply-tp7583405.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo


-- 
Oscar Perpiñán Lamigueiro
Grupo de Sistemas Fotovoltaicos (IES-UPM)
Dpto. Ingeniería Eléctrica (EUITI-UPM)
URL: http://procomun.wordpress.com
Twitter: @oscarperpinan



More information about the R-sig-Geo mailing list