| maskNA,GRaster-method | R Documentation |
This function work in two ways:
If byLayer = TRUE (default), it will convert 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. Regardless, if the input is a "stack" of GRasters, it will return a stack with the same number of raster layers.
If byLayer = FALSE and x is a "stack" of GRasters,, it will return a single GRaster layer. This layer will have a user-defined value in all cells that had no NAs across all rasters, and NAs in cells where at least one raster had an NA. This is useful for masking out areas where data are missing in any of the rasters. If invert = TRUE, it will return a single GRaster layer with a user-defined value in all cells that had at least one NA across all rasters, and NAs in cells where all rasters had non-NA values.
## S4 method for signature 'GRaster'
maskNA(x, value = 1, invert = FALSE, retain = FALSE, byLayer = TRUE)
x |
A |
value |
Numeric: Value to which to assign to masked cells. The default is 1. |
invert |
Logical: If |
retain |
Logical: If |
byLayer |
Logical: If |
A GRaster.
not.na(), app(), mask()
if (grassStarted()) {
# Setup
library(terra)
### Mask layer-by-layer
# 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))
### Mask *across* layers
# Load forest raster, which has many NAs
madForest2000 <- fastData("madForest2000")
forest2000 <- fast(madForest2000)
x <- c(elev, forest2000)
maskByLayer <- maskNA(x)
maskAcrossLayers <- maskNA(x, byLayer = FALSE)
maskByLayer
maskAcrossLayers
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.