| crossFreq,GRaster-method | R Documentation |
crossFreq() tabulates the number of cells across two or more rasters for each combination of values in the rasters. Only cells that are not NA across all rasters will be used.
## S4 method for signature 'GRaster'
crossFreq(x, na.rm = TRUE, cats = TRUE, verbose = FALSE)
x |
A stack of integer/categorical |
na.rm |
Logical: If |
cats |
Logical: If |
verbose |
Logical: If |
A data.frame or a named list of data.frames, one per each pair of rasters in x.
freq(), GRASS tool r.stats (see grassHelp("r.stats"))
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev") # raster
madCover <- fastData("madCover") # categorical raster
# Convert to GRasters
elev <- fast(madElev) # integer raster
cover <- fast(madCover) # categorical raster
# Frequencies of integer raster values
f1 <- freq(elev)
print(f1) # have to do this sometimes if output is a data table
# Frequencies of categorical raster values
f2 <- freq(cover)
print(f2) # have to do this sometimes if output is a data table
# Frequencies of given values
f3 <- freq(elev, value = 4)
print(f3) # have to do this sometimes if output is a data table
# When a GRaster has non-integer values, they will be binned:
f4 <- freq(elev + 0.1, bins = 10)
print(f4)
# Calculate cross frequencies between rasters... both need to be integer.
elevWgs84 <- project(elev, cover)
elevClasses <- clump(elevWgs84, minDiff = 0.13) # bin elevations
names(elevClasses) <- 'elevClass'
f5 <- crossFreq(c(elevClasses, cover), na.rm = FALSE)
print(f5) # have to do this sometimes if output is a data table
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.