[R-sig-Geo] raster package - zonal - standard deviations

Christian Levers christian.levers at geo.hu-berlin.de
Tue Nov 6 17:59:23 CET 2012


Thanks a lot Robert!

Indeed it's (obviously) a matter of memory (although our server has 
approx 200GB RAM). While simulating RAM limitations, I obtain the same 
message: error in .local(x, z, ...) : stat can be 'sum', 'mean', 'min', 
or 'max'.

Changing maxmemory doesn't improve things, even with 1E+20. I was 
thinking on looping through the zones (my layer has just 460) and using 
cellStats, too.

Thanks again and all the best from Berlin,
Christian


Am 05.11.2012 19:11, schrieb Robert J. Hijmans:
>
> The small example can be processed in memory, so any sensible function
> will work.
>
> library(raster)
> r <- raster(ncols=10, nrows=10)
> r[] <- runif(ncell(r)) * 1:ncell(r)
> z <- r
> z[] <- rep(1:5, each=20)
> zonal(r, z, 'sd')
>
> # simulate RAM limitation:
> setOptions(todisk=TRUE)
> zonal(r, z, 'sd')
> # don't forget to turn this off.
> setOptions(todisk=FALSE)
>
> # perhaps you can overcome that by changing "maxmemory". See
> showOptions()
> setOptions(maxmemory=1e+10)
>
>
> I will also add an "sd" function to zonal for large rasters. That is not
> as simple as for min, max, and sum, but it can be done, as it is done in
> cellStats. If you do not have too many zones, you could, for now, use a
> loop with a combination of reclass/mask/cellStats to extract the sd for
> each zone.
>
> Robert
>
>
>
> On Mon, Nov 5, 2012 at 7:35 AM, Rafael Wüest <rafael.wueest at wsl.ch
> <mailto:rafael.wueest at wsl.ch>> wrote:
>
>     Hi Christian
>
>     What version of raster do you use. Using my installation of raster
>     (version 2.0-12), 'sd' as a function in zonal works fine (see
>     example below).
>
>     r <- raster(ncols=10, nrows=10)
>     r[] <- runif(ncell(r)) * 1:ncell(r)
>     z <- r
>     z[] <- rep(1:5, each=20)
>     zonal(r, z, 'sd')
>           zone      "sd"
>     [1,]    1  4.894058
>     [2,]    2  9.216353
>     [3,]    3 14.144341
>     [4,]    4 19.973913
>     [5,]    5 27.934773
>
>     I guess updating the raster package will solve the problem.
>
>     Best,
>     Rafael
>
>     On 02.11.2012, at 18:35, Christian Levers wrote:
>
>      > Hello,
>      >
>      > I am using the raster package to calculate zonal statistics for
>     my stack of ca. 50 rasters (each raster is about 33 mio. pixels) and
>     I am wondering if there is a simple way to calculate standard
>     deviations for the zones since "sd" is not implemented in the
>     presets for the "stat" argument. If not, I guess defining a function
>     which calculates std.dev is the easiest solution.
>      >
>      > Thanks in advance and a nice weekend,
>      > Christian
>      >
>      > --
>      > MSc Geogr. Christian Levers
>      > Doctoral Researcher
>      >
>      > Biogeography & Conservation Biology
>      > Geography Department | Humboldt-Universitaet zu Berlin
>      >
>      > eMail: christian.levers at geo.hu-berlin.de
>     <mailto:christian.levers at geo.hu-berlin.de>
>      > phone: +49 (0) 30 2093 9341 <tel:%2B49%20%280%29%2030%202093%209341>
>      > fax: +49 (0) 30 2093 6848 <tel:%2B49%20%280%29%2030%202093%206848>
>      > mail:   Unter den Linden 6 | 10099 Berlin | Germany
>      > web: http://www.geographie.hu-berlin.de
>      >
>      > _______________________________________________
>      > R-sig-Geo mailing list
>      > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>      > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
>
>     --
>     Rafael Wüest
>     Swiss Federal Research Institute WSL
>     Zürcherstrasse 111
>     8903 Birmensdorf
>     Switzerland
>
>     +41 44 7392126 <tel:%2B41%2044%207392126>
>     rafael.wueest at wsl.ch <mailto:rafael.wueest at wsl.ch>
>     http://www.wsl.ch/info/mitarbeitende/wueest/index_EN
>
>     _______________________________________________
>     R-sig-Geo mailing list
>     R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
>     https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>


-- 
MSc Geogr. Christian Levers
Doctoral Researcher

Biogeography & Conservation Biology
Geography Department | Humboldt-Universitaet zu Berlin

eMail:  christian.levers at geo.hu-berlin.de
phone:  +49 (0) 30 2093 9341
fax:    +49 (0) 30 2093 6848
mail:   Unter den Linden 6 | 10099 Berlin | Germany
web:    http://www.geographie.hu-berlin.de



More information about the R-sig-Geo mailing list