These functions can be applied to a "stack" of GRaster
s with two or more layers. They return a single-layered GRaster
. If you want to summarize across cells in a raster (e.g., calculate the mean value of all cells on a raster), use global()
. Options include:
Numeration: count()
(number of non-NA
cells), sum()
.
Extremes: min()
, max()
, which.min()
(index of raster with the minimum value), which.max()
(index of the raster with the maximum value)
Dispersion: range()
, stdev()
(standard deviation), var()
(sample variance), varpop()
(population variance), nunique()
(number of unique values), quantile()
(use argument probs
), skewness()
, and kurtosis()
.
NA
s: anyNA()
(any cells are NA
?), allNA()
(are all cells NA
?)
# S4 method for GRaster
mean(x, na.rm = FALSE)# S4 method for GRaster
mmode(x, na.rm = FALSE)
# S4 method for GRaster
median(x, na.rm = FALSE)
# S4 method for GRaster
count(x)
# S4 method for GRaster
sum(x, na.rm = FALSE)
# S4 method for GRaster
min(x, na.rm = FALSE)
# S4 method for GRaster
max(x, na.rm = FALSE)
# S4 method for GRaster
which.min(x)
# S4 method for GRaster
which.max(x)
# S4 method for numeric
sdpop(x, na.rm = FALSE)
# S4 method for GRaster
varpop(x, na.rm = FALSE)
# S4 method for numeric
varpop(x, na.rm = FALSE)
# S4 method for GRaster
stdev(x, pop = TRUE, na.rm = FALSE)
# S4 method for GRaster
var(x, na.rm = FALSE)
# S4 method for GRaster
nunique(x, na.rm = FALSE)
# S4 method for GRaster
skewness(x, na.rm = FALSE)
# S4 method for GRaster
kurtosis(x, na.rm = FALSE)
# S4 method for GRaster
range(x, na.rm = FALSE)
# S4 method for GRaster
quantile(x, prob, na.rm = FALSE)
# S4 method for GRaster
anyNA(x)
# S4 method for GRaster
allNA(x)
A GRaster
.
A GRaster
. Typically, this raster will have two or more layers. Values will be calculated within cells across rasters.
Logical: If FALSE
(default), of one cell value has an NA
, the result will be NA
. If TRUE
, NA
s are ignored.
Logical (for stdev()
): If TRUE
(default), calculate the population standard deviation across layers. If FALSE
, calculate the sample standard deviation.
Numeric: Quantile to calculate. Used for quantile()
.
if (grassStarted()) {
# Setup
library(sf)
library(terra)
# Example data
madChelsa <- fastData("madChelsa")
# Convert a SpatRaster to a GRaster
chelsa <- fast(madChelsa)
chelsa # 4 layers
# Central tendency
mean(chelsa)
mmode(chelsa)
median(chelsa)
# Statistics
nunique(chelsa)
sum(chelsa)
count(chelsa)
min(chelsa)
max(chelsa)
range(chelsa)
skewness(chelsa)
kurtosis(chelsa)
stdev(chelsa)
stdev(chelsa, pop = FALSE)
var(chelsa)
varpop(chelsa)
# Which layers have maximum/minimum?
which.min(chelsa)
which.max(chelsa)
# Regression
# Note the intercept is different for fasterRaster::regress().
regress(chelsa)
regress(madChelsa, 1:nlyr(madChelsa))
# Note: To get quantiles for each layer, use global().
quantile(chelsa, 0.1)
# NAs
madForest2000 <- fastData("madForest2000")
forest2000 <- fast(madForest2000)
forest2000 <- project(forest2000, chelsa, method = "near")
chelsaForest <- c(chelsa, forest2000)
nas <- anyNA(chelsaForest)
plot(nas)
allNas <- allNA(chelsaForest)
plot(allNas)
}
Run the code above in your browser using DataLab