zonal,GRaster,ANY-method | R Documentation |
Function zonal()
calculates statistics (mean, sum, etc.) on cells of a GRaster
by "zones" created by cells of another GRaster
or GVector
.
## S4 method for signature 'GRaster,ANY'
zonal(x, z, fun = "mean", probs = 0.5)
x |
A |
z |
A
|
fun |
Character vector: Name of the function(s) to summarize
|
probs |
Numeric: Quantile at which to calculate |
A data.frame
or data.table
.
if (grassStarted()) {
# Setup
library(terra)
# Elevation SpatRaster:
madElev <- fastData("madElev")
# Convert a SpatRaster to a GRaster:
elev <- fast(madElev)
### Calculate zonal statistics using a GRaster as zones
#######################################################
# Generate a "zones" GRaster by dividing raster into areas based on
# high/low elevation.
names(elev) # Use this name in app() formula.
fun <- "= if (madElev <200, 0, if (madElev <400, 1, 2))"
zones <- app(elev, fun = fun)
# Calculate zonal statistics using a raster as zones
zonal(elev, zones, fun = "mean")
zonal(elev, zones, fun = "*") # all statistics
# Calculate zonal statistics on multi-layered GRaster
elev2 <- c(elev, log10(elev))
zonal(elev2, zones, fun = c("mean", "sum", "sdpop"))
### Calculate zonal statistics using a GVector as zones
#######################################################
madCoast4 <- fastData("madCoast4")
coast <- fast(madCoast4)
zonal(elev, z = coast, fun = "mean")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.