This function replaces one or more user-specified values in a raster with other values. See classify() for replacing ranges of values.
Arguments
- x
A
GRaster.- from, to
Vectors of numeric or character values. The value(s) in
fromwill be replaced with the value(s) into. They must be the same length, or, if you supply a single value forto, then all values infromwill be converted to the same value ofto. Numeric/integer or character vectors can be used:fromandtoare numeric or integer vectors: Values infromwill be replaced by their corresponding value into.fromandtoare character vectors: You can use a character vector forfromandto. In this case, the input raster must be a factor (categorical) raster or an integer raster. Iffromis a character vector, these levels(categories) will be replaced by the levels into.This can add levels to aGRastertohas labels that do not match any existing labels infrom.fromis a character vector andtois an integer vector: Cells inxthat correspond to the given label will have their values replaced by the corresponding value into, and be matched the the corresponding label. If no label corresponds to the new value, a new level will be created. The input must be a categorical raster.fromis an integer vector andtois a character vector: Cells inxthat have a value infromwill be replaced by values that match the labels into. If the input raster does not have a value that corresponds to the given label iny, then a new value will be created.
- others
NULL(default),NA, numeric, or a character:NULL(default): Values that do not appear infromwill be unchanged.NA: Values that do not appear infromwill be set toNA.Character: Cells in
xthat do not appear intowill be assigned to this level. In this case,xmust be a categorical (factor) raster.
- warn
Logical: If
TRUE(default), display a warning when new levels are created.
Examples
if (grassStarted()) {
# Setup
library(terra)
# Example data
madElev <- fastData("madElev")
madCover <- fastData("madCover")
### Substitution within an integer/numeric raster
# Convert SpatRaster to GRaster
elev <- fast(madElev)
# Simple substitution of one value, keeping all other values
newElev <- elev
newElev[newElev == 100] <- -100
newElev[newElev > 500] <- 500
hist(newElev)
# Simple substitution of one value, keeping all other values
substituted <- subst(elev, from = 300, to = -300)
substituteds <- c(elev, substituted)
names(substituteds) <- c("original", "substituted")
plot(substituteds)
# Simple substitution of three values, keeping all other values
substituted <- subst(elev, from = c(299, 300, 301), to = c(-699, -600, -601))
substituteds <- c(elev, substituted)
names(substituteds) <- c("original", "substituted")
plot(substituteds)
# Simple substitution of three values to one other value, retaining remainder
substituted <- subst(elev, from = c(299, 300, 301), to = -1000)
substituteds <- c(elev, substituted)
names(substituteds) <- c("original", "substituted")
plot(substituteds)
# Simple substitution of one value, setting all other values to 100
substituted <- subst(elev, from = 300, to = -300, others = 100)
substituteds <- c(elev, substituted)
names(substituteds) <- c("original", "substituted")
plot(substituteds)
### Substitution within a factor/categorical raster
# Convert a SpatRaster to a GRaster:
cover <- fast(madCover)
cover <- droplevels(cover) # remove unused levels
levels(cover) # levels of "cover"
# Substitute using level name, replace with EXISTING level label
from <- "Mosaic cropland/vegetation"
to <- "Mosaic crops"
categ <- subst(cover, from = from, to = to)
freq(cover) # original frequencies of each land cover class
freq(categ) # note change in frequency of "from" and "to" categories
plot(c(cover, categ))
# Substitute using level name, replace with NEW level label
from <- c("Mosaic crops", "Mosaic cropland/vegetation")
to <- c("Mixed cropland")
categ <- subst(cover, from = from, to = to)
freq(cover) # original frequencies of each land cover class
freq(categ) # note change in frequency of "from" and "to" categories
plot(c(cover, categ))
# Substitute using level name, replace with NEW level label
from <- c("Mosaic crops", "Mosaic cropland/vegetation")
to <- c("Mixed cropland", "Mixed cropland/vegetation")
categ <- subst(cover, from = from, to = to)
freq(cover) # original frequencies of each land cover class
freq(categ) # note change in frequency of "from" and "to" categories
plot(c(cover, categ))
# Substitute using level name, replace with VALUE of an existing label
from <- c("Mosaic crops", "Mosaic cropland/vegetation")
to <- 120
categ <- subst(cover, from = from, to = to)
freq(cover) # original frequencies of each land cover class
freq(categ) # note change in frequency of "from" and "to" categories
plot(c(cover, categ))
# Substitute using level name, replace with new level name, replace all others
from <- c("Mosaic crops", "Mosaic cropland/vegetation")
to <- "Crops"
categ <- subst(cover, from = from, to = to, others = "Other")
freq(cover) # original frequencies of each land cover class
freq(categ) # note change in frequency of "from" and "to" categories
plot(c(cover, categ))
}
