[R-sig-Geo] raster: apply() in brick objects?

Agustin Lobo alobolistas at gmail.com
Fri Nov 26 15:46:12 CET 2010


Hi!

Given a brick object (longitude, latitude, time), I need a (lon,lat)
layer with the maximum value of each cell across time.
That's  the equivalent to
> x = array(round(runif(n=3*4*5,min=0,max=10)),dim=c(3,4,5))
> apply(x,c(1,2),max,na.rm=T)

but for x being a brick object. How can I do it with raster?

I've tried

> funmax <- function(x) {apply(x,c(1,2),max,na.rm=T)}
> show(br)
class       : RasterBrick
filename    :
nlayers     : 3
nrow        : 5
ncol        : 5
ncell       : 25
projection  : NA
min value   : ? ? ?
max value   : ? ? ?
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
resolution  : 0.2, 0.2  (x, y)
> brmax <- calc(br,funmax)

But the result is wrong, as it should be 1 layer and not 3:
> show(brmax)	
class       : RasterBrick
filename    :
nlayers     : 3
nrow        : 5
ncol        : 5
ncell       : 25
projection  : NA
min value   : 232 232 248
max value   : 248 248 248
extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
resolution  : 0.2, 0.2  (x, y)

I've also tried with stackApply(), but do not think that's the way to go.
Also, I'm concerned that apply() is typically very slow in R, and I'm
dealing with a large brick object.

Thanks

Agus



More information about the R-sig-Geo mailing list