zonalDT: Speedy Zonal Statistics

View source: R/zonalDT.R

zonalDTR Documentation

Speedy Zonal Statistics


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


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



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


A categorical RasterLayer with codes representing zones.


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


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


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


Joshua O'Brien


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

rasterDT documentation built on Dec. 28, 2022, 1:57 a.m.