raster (version 1.0.0-1)

zonal: Zonal statistics

Description

Compute zonal statistics. That is, cross-tabulate the values of a RasterLayer based on a "zones" RasterLayer.

Usage

zonal(raster, zones, stat='mean', na.rm=TRUE, progress)

Arguments

raster
A RasterLayer object
zones
A RasterLayer object; with codes representing zones that are treated as integer values
stat
The function to be applied. Either as character: 'mean', 'min', 'max', 'sum'; or a function (see Details)
na.rm
Logical. If TRUE, NA will be removed from the computations and the result will only be NA if all cells in a zone are NA
progress
Character. Valid values are "text", "tcltk", "windows" (on that platform only) and ""

Value

  • A data frame with a value for each zone

Details

If stat is a function, zonal will fail (gracefully) for very large RasterLayers

Examples

Run this code
r <- raster(ncols=10, nrows=10)
r[] <- runif(ncell(r)) * 1:ncell(r)
z <- r
z[] <- rep(1:5, each=20)
zonal(r, z, 'mean')
zonal(r, z, 'min')
zonal(r, z, 'sum')
# using a function, rather than a character value
zonal(r, z, sum)

Run the code above in your browser using DataCamp Workspace