This function calculates statistics on a moving "neighborhood" of cells of a raster. The neighborhood can be a square, circle, or a user-defined set of cells (with or without weights).
Arguments
- x
A
GRaster
.- w
Numeric integer or a square matrix with an odd number of rows and columns: The size and nature of the neighborhood:
"Square" neighborhoods (when
circle = FALSE
): An odd integer >= 3, indicating indicates the size of a "square" neighborhood (number of cells wide and number or cells tall)."Circular" neighborhoods (when
circle = TRUE
): An odd integer >=3, indicating the diameter of the circle.A matrix of cell weights: The matrix must be square and have an odd number of rows and columns (example:
matrix(c(0.5, 1, 0.5, 1, 2, 1, 0.5, 1, 0.5), nrow=3)
). You cannot use a weights matrix whencircle = TRUE
. Cells withNA
as a weight will be ignored. Note that weighted matrices should not be used for functionmin
,max
,count
,nunique
, orinterspersion
.
- fun
Character: Name of the function to apply to the neighborhood:
"
mean
" (default)"
median
""
mode
""
min
" or "max
": Minimum or maximum. Should not use a weights matrix."
range
": Difference between the maximum and minimum. Should not use a weights matrix."
sd
": Sample standard deviation. NB: This is the same as thestats::sd()
function."
sdpop
": Population standard deviation. NB: This is the same as the function "stddev" in the GRASS moduler.neighbors
."
sum
": Sum of non-`NAβ cells."
count
": Number of non-`NA cells. Should not use a weights matrix."
var
": Sample variance. NB: This is the same as thestats::var()
function."
varpop
": Population variance. NB: This is the same as the function "variance" in the GRASS moduler.neighbors
."
nunique
": Number of unique values. Should not use a weights matrix."
interspersion
": Proportion of cells with values different from focal cell (e.g., if 6 of 8 cells have different values, then the interspersion is 6/8 = 0.75). NB: This is slightly different from how it is defined in the GRASS moduler.neighbors
. Should not use a weights matrix."
quantile
": Quantile of values. The value in argumentquantile
is used to specify the quantile.
The center cell value is always included in the calculations, and all calculations ignore
NA
cells (i.e., they do not count as cells in the focal neighborhood).- circle
Logical: If
FALSE
(default), use a square neighborhood. IfTRUE
, use a circular neighborhood. When this isTRUE
, argumentw
cannot be a matrix.- quantile
Numeric between 0 and 1, inclusive: Quantile to calculate when
fun = "quantile"
. The default value is 0.5 (median), and valid values must be in the range between 0 and 1, inclusive.
See also
terra::focal()
, GRASS manual page for module r.neighbors
(see grassHelp("r.neighbors")
)
Examples
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev")
# Convert a SpatRaster to a GRaster:
elev <- fast(madElev)
# Focal calculations:
sums <- focal(elev, fun = "sum")
means <- focal(elev, fun = "mean")
# Focal calculations on a circular window:
sds <- focal(elev, fun = "sd") # square
sdsCircle <- focal(elev, fun = "sd", circle = TRUE) # circle
sds
sdsCircle
plot(sds - sdsCircle)
# Focal calculations with user-defined weights:
w <- matrix(c(1, 0, 1, 0, 1, 0, 1, 0, 1), ncol = 3)
w
sumsWeighted <- focal(elev, fun = "sum", w = w)
s <- c(sums, sumsWeighted)
minmax(s)
}