functions: Mathematical operations on two or more GRasters

mean,GRaster-methodR Documentation

Mathematical operations on two or more GRasters

Description

These functions can be applied to a "stack" of GRasters 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().

  • Central tendency: mean(), mmode() (mode), median().

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

  • NAs: anyNA() (any cells are NA?), allNA() (are all cells NA?)

Usage

## S4 method for signature 'GRaster'
mean(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
mmode(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
median(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
count(x)

## S4 method for signature 'GRaster'
sum(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
min(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
max(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
which.min(x)

## S4 method for signature 'GRaster'
which.max(x)

## S4 method for signature 'numeric'
sdpop(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
varpop(x, na.rm = FALSE)

## S4 method for signature 'numeric'
varpop(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
stdev(x, pop = TRUE, na.rm = FALSE)

## S4 method for signature 'GRaster'
var(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
nunique(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
skewness(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
kurtosis(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
range(x, na.rm = FALSE)

## S4 method for signature 'GRaster'
quantile(x, prob, na.rm = FALSE)

## S4 method for signature 'GRaster'
anyNA(x)

## S4 method for signature 'GRaster'
allNA(x)

Arguments

x

A GRaster. Typically, this raster will have two or more layers. Values will be calculated within cells across rasters.

na.rm

Logical: If FALSE (default), of one cell value has an NA, the result will be NA. If TRUE, NAs are ignored.

pop

Logical (for stdev()): If TRUE (default), calculate the population standard deviation across layers. If FALSE, calculate the sample standard deviation.

prob

Numeric: Quantile to calculate. Used for quantile().

Value

A GRaster.

Examples

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(x, "quantile", probs = 0.2).
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)

}

adamlilith/fasterRaster documentation built on Oct. 26, 2024, 4:06 p.m.