zonal: Zonal statistics

Description Usage Arguments Value See Also Examples

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

1
2
## S4 method for signature 'SpatRaster,SpatRaster'
zonal(x, z, fun=mean, ..., as.raster=FALSE, filename="", wopt=list()) 

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

as.raster

logical. If TRUE, a SpatRaster is returned with the zonal statistic for each zone

filename

character. Output filename (ignored if as.raster=FALSE

wopt

list with additional arguments for writing files as in writeRaster

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)
z <- rast(r)
values(z) <- rep(c(1:2, NA, 3:4), each=20)
names(z) <- "zone"
zonal(r, z, "sum", na.rm=TRUE)

# multiple layers
r <- rast(system.file("ex/logo.tif", package = "terra")) 
# zonal layer 
z <- rast(r, 1)
names(z) <- "zone"
values(z) <- rep(c(1:2, NA, c(3:4)), each=ncell(r)/5, length.out=ncell(r))

zonal(r, z, "mean", na.rm = TRUE)

# raster of zonal values
zr <- zonal(r, z, "mean", na.rm = TRUE, as.raster=TRUE)

terra documentation built on Aug. 20, 2021, 9:07 a.m.