zonalDT {rasterDT}R Documentation

Speedy Zonal Statistics

Description

A fast data.table-based alternative to raster::zonal().

Usage

zonalDT(x, z, fun = sum, na.rm = TRUE)

Arguments

x

A Raster* to the totality of whose values fun should be applied within each zone.

z

A categorical RasterLayer with codes representing zones.

fun

A name or character string giving the function to be applied to summarize the values by zone. It needs to return a single (or at least a length-one vector). If x might contain any NA values, it should be equipped to handle them. For large rasters, this function needs to be one, like sum() whose value is the same even if carried out in a two-stage application (i.e. first to data subsets and then to the results of those subset applications).

na.rm

Logical. If TRUE, NA values in x are ignored.

Value

A data.table with a summary value for each zone.

Author(s)

Joshua O'Brien

Examples

r <- raster(ncols = 10, nrows = 10)
r[] <- runif(ncell(r)) * 1:ncell(r)
z <- r
z[] <- rep(1:5, each = 20)
## for big files, use a character value rather than a function
zonalDT(r, z, "sum")

## for smaller files you can also provide a function
zonalDT(r, z, mean)
zonalDT(r, z, min)

## multiple layers
zonalDT(stack(r, r*10), z, "sum")

[Package rasterDT version 0.3.2 Index]