Learn R Programming

terra (version 0.2-7)

zonal: Zonal statistics

Description

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

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

# S4 method for SpatRaster,SpatRaster
zonal(x, z, fun="mean", na.rm=TRUE, ...)

Arguments

x

SpatRaster

z

SpatRaster with values 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 SpatRasters, a proper function

na.rm

logical. If TRUE, NA values in x are ignored

...

Additional arguments. None implemented

Value

A matrix 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), reduce for local statistics, and extract for summarizing values for polygons

Examples

Run this code
# NOT RUN {
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")
# }

Run the code above in your browser using DataLab