Learn R Programming

enmSdmX (version 1.2.12)

globalx: "Friendly" wrapper for terra::global() for calculating raster statistics

Description

Calculate "global" statistics across all the values in a raster. This function is a wrapper for global. That function, by default, sets na.rm = FALSE, so any cell that is NA can cause the summary statistic to also be NA (usually undesirable). The function also returns a data.frame, so often needs a further line of code to get the actual value(s). This function sets na.rm = TRUE by default, and returns a numeric vector (not a data.frame).

Usage

globalx(x, fun, na.rm = TRUE, ..., weights = NULL)

Value

A numeric vector, one value per layer in x.

Arguments

x

A SpatRaster.

fun

A function or the name of a function (in quotes). See global for more details.

na.rm

If TRUE (default), then the function in fun will ignore NA cells.

...

Additional arguments to pass to fun.

weights

Either NULL or a SpatRaster.

Examples

Run this code

library(terra)

r <- rast(ncols=10, nrows=10)
values(r) <- 1:ncell(r)


global(r, 'sum') # terra
globalx(r, 'sum') # enmSdmX

global(r, "mean", na.rm=TRUE)[1, 1] # terra... same as enmSdmX::globalx

Run the code above in your browser using DataLab