Description Usage Arguments Value See Also Examples

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)

1 2 3 4 5 |

`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 |

`...` |
additional arguments. One implemented: |

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

)

See `cellStats`

for 'global' statistics (i.e., all of `x`

is considered a single zone), and `extract`

for summarizing values for polygons

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')
``` |

Questions? Problems? Suggestions? Tweet to @rdrrHQ or email at ian@mutexlabs.com.

Please suggest features or report bugs in the GitHub issue tracker.

All documentation is copyright its authors; we didn't write any of that.

Embedding an R snippet on your website

Add the following code to your website.

For more information on customizing the embed code, read Embedding Snippets.