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

Robert J. Hijmans r.hijmans at gmail.com
Fri Nov 26 20:12:26 CET 2010


Or this:

brmax <- max(br)


On Fri, Nov 26, 2010 at 7:01 AM, Agustin Lobo <alobolistas at gmail.com> wrote:
> ...well, it's Friday...
>
> it's actually just
>> brmax <- calc(br,max,na.rm=T)
>> show(brmax)
> class       : RasterBrick
> filename    :
> nlayers     : 1
> nrow        : 5
> ncol        : 5
> ncell       : 25
> projection  : NA
> min value   : 248
> max value   : 248
> extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
> resolution  : 0.2, 0.2  (x, y)
>
> that simple.
>
> Agus
>
> 2010/11/26 Agustin Lobo <alobolistas at gmail.com>:
>> 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