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

Usage

## S4 method for signature 'RasterLayer,RasterLayer'
zonal(x, z, fun='mean', digits=0, na.rm=TRUE, ...) 

## S4 method for signature 'RasterStackBrick,RasterLayer'
zonal(x, z, fun='mean', digits=0, na.rm=TRUE, ...)

Arguments

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 TRUE, NA values in x are ignored

...

additional arguments. One implemented: progress, as in writeRaster

Value

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

See Also

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

Examples

r <- raster(ncols=10, nrows=10)
values(r) <- runif(ncell(r)) * 1:ncell(r)
z <- r
values(z) <- rep(1:5, each=20)
# for large 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')

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

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