This function converts all non-NA
cells in a GRaster
to a single user-defined value, leaving NA
cells as NA
. Alternatively, it can convert NA
cells to a user-defined value, and all non-NA
cells to NA.
Arguments
- x
A
GRaster
.- value
Numeric: Value to which to assign to masked cells. The default is 1.
- invert
Logical: If
FALSE
(default), convert non-NA
cells tovalue
, and leaveNA
cells as-is. IfTRUE
, convert allNA
cells tovalue
, and non-NA
cells toNA
.- retain
Logical: If
invert
isTRUE
andretain
isFALSE
(default), non-NA
cells will retain their value. This argument is ignored ifinvert
isFALSE
.
Examples
if (grassStarted()) {
# Setup
library(terra)
# Elevation raster
madElev <- fastData("madElev")
# Convert a SpatRaster to a GRaster:
elev <- fast(madElev)
# Convert non-NA to 1, NA cells remain NA
elevMask <- maskNA(elev)
elevMask
plot(c(elev, elevMask))
# Convert NA to 1, non-NA cells become NA
elevInvertMask <- maskNA(elev, invert = TRUE)
elevInvertMask
plot(c(elev, elevInvertMask))
# Convert NA to 200, non-NA cells keep their values
elevInvertRetain <- maskNA(elev, value = 200, invert = TRUE, retain = TRUE)
elevInvertRetain
plot(c(elev, elevInvertRetain))
}