# zonal: Zonal statistics In raster: Geographic Data Analysis and Modeling

## Description

Compute zonal statistics, that is summarized values of a Raster* object for each "zone" defined by a RasterLayer.

If `stat` is a true `function`, `zonal` will fail (gracefully) for very large Raster objects, but it will in most cases work for functions that can be defined as by a character argument ('mean', 'sd', 'min', 'max', or 'sum'). In addition you can use 'count' to count the number of cells in each zone (only useful with `na.rm=TRUE`, otherwise `freq(z)` would be more direct.

If a function is used, it should accept a `na.rm` argument (or at least a `...` argument)

## Usage

 ```1 2 3 4 5``` ```## S4 method for signature 'RasterLayer,RasterLayer' zonal(x, z, fun='mean', digits=0, na.rm=TRUE, ...) ## S4 method for signature 'RasterStackBrick,RasterLayer' zonal(x, z, fun='mean', digits=0, na.rm=TRUE, ...) ```

## Arguments

 `x` Raster* object `z` RasterLayer with codes representing zones `fun` function to be applied to summarize the values by zone. Either as character: 'mean', 'sd', 'min', 'max', 'sum'; or, for relatively small Raster* objects, a proper function `digits` integer. Number of digits to maintain in 'zones'. By default averaged to an integer (zero digits) `na.rm` logical. If `TRUE`, `NA` values in `x` are ignored `...` additional arguments. One implemented: `progress`, as in `writeRaster`

## Value

A matrix with a value for each zone (unique value in `zones`)

## See Also

See `cellStats` for 'global' statistics (i.e., all of `x` is considered a single zone), and `extract` for summarizing values for polygons

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16``` ```r <- raster(ncols=10, nrows=10) r[] <- runif(ncell(r)) * 1:ncell(r) z <- r z[] <- rep(1:5, each=20) # for big files, use a character value rather than a function zonal(r, z, 'sum') # for smaller files you can also provide a function ## Not run: zonal(r, z, mean) zonal(r, z, min) ## End(Not run) # multiple layers zonal(stack(r, r*10), z, 'sum') ```

### Example output

```Loading required package: sp
NOTE: rgdal::checkCRSArgs: no proj_defs.dat in PROJ.4 shared files
zone      sum
[1,]    1 117.0794
[2,]    2 339.9061
[3,]    3 518.1140
[4,]    4 721.4737
[5,]    5 909.4851
zone    value
[1,]    1  5.85397
[2,]    2 16.99530
[3,]    3 25.90570
[4,]    4 36.07369
[5,]    5 45.47426
zone     value
[1,]    1 0.4004358
[2,]    2 2.9115227
[3,]    3 1.4214003
[4,]    4 8.0294389
[5,]    5 0.1334188
NOTE: rgdal::checkCRSArgs: no proj_defs.dat in PROJ.4 shared files
zone  layer.1  layer.2
[1,]    1 117.0794 1170.794
[2,]    2 339.9061 3399.061
[3,]    3 518.1140 5181.140
[4,]    4 721.4737 7214.737
[5,]    5 909.4851 9094.851
```

raster documentation built on Nov. 17, 2017, 5:51 a.m.