segregate,GRaster-method | R Documentation |
This function creates a multi-layered GRaster
for every unique values in an input GRaster
. By default, the output will have a value of 1 wherever the input has the given value, and 0 elsewhere. This is useful for creating dummy variable GRaster
layers for use with models that have factors, especially if the input GRaster
is categorical. Note that the predict()
function in fasterRaster usually does not need this treatment of GRaster
s since it can handle categorical rasters already.
## S4 method for signature 'GRaster'
segregate(x, classes = NULL, keep = FALSE, other = 0, bins = 100, digits = 3)
x |
A |
classes |
Either |
keep |
Logical: If |
other |
Numeric or |
bins |
Numeric: Number of bins in which to put values. This is only used for |
digits |
Numeric: Number of digits to which to round input if it is a |
If the input x
is a single-layered GRaster
, the output will be a multi-layered GRaster
with one layer per value in the input, or one layer per values in classes
. If the input is a multi-layered GRaster
, the output will be a list
of multi-layered GRaster
s.
terra::segregate()
if (grassStarted()) {
# Setup
library(terra)
# Elevation and land cover raster
madElev <- fastData("madElev") # integer raster
madCover <- fastData("madCover") # categorical raster
# Convert to GRasters
elev <- fast(madElev)
cover <- fast(madCover)
# Subset elevation raster to just a few values to make example faster:
elevSubset <- elev[elev <= 3]
segregate(elevSubset)
segregate(elevSubset, keep = TRUE, other = -1)
# Segregate the factor raster
segregate(cover)
classes <- c("Grassland with mosaic forest", "Mosaic cropland/vegetation")
seg <- segregate(cover, classes = classes)
plot(seg)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.