Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

zonal

Zonal statistics


Description

Compute zonal statistics, that is summarized values of a SpatRaster for each "zone" defined by another SpatRaster.

If fun is a true function, zonal may fail for very large SpatRaster objects, except for the functions ("mean", "min", "max", or "sum").

Usage

## S4 method for signature 'SpatRaster,SpatRaster'
zonal(x, z, fun=mean, ...)

Arguments

x

SpatRaster

z

SpatRaster with values representing zones

fun

function to be applied to summarize the values by zone. Either as character: "mean", "min", "max", "sum", or, for relatively small SpatRasters, a proper function

...

additional arguments passed to fun

Value

A data.frame with a value for each zone (unique value in zones)

See Also

See global for "global" statistics (i.e., all of x is considered a single zone), app for local statistics, and extract for summarizing values for polygons

Examples

r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)
z <- rast(r)
values(z) <- rep(1:4, each=25)
zonal(r, z, "sum", na.rm=TRUE)

# multiple layers
r <- rast(system.file("ex/logo.tif", package = "terra")) 
# zonal layer 
z <- rast(r, 1)
values(z) <- rep(1:4, each=ncell(r)/4, len=ncell(r))
zonal(r, z, "mean", na.rm = TRUE)

terra

Spatial Data Analysis

v1.2-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Roger Bivand [ctb] (<https://orcid.org/0000-0003-2392-6140>), Karl Forner [ctb], Jeroen Ooms [ctb] (<https://orcid.org/0000-0002-4035-0289>), Edzer Pebesma [ctb] (<https://orcid.org/0000-0001-8049-7069>)
Initial release
2021-05-12

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.