na.omit: Force cells with NA in any layer of a GRaster stack to NA

na.omit,GRaster-methodR Documentation

Force cells with NA in any layer of a GRaster stack to NA

Description

'na.omit()' returns a GRaster stack with the same number of layers as the input, but with all cells that have NA in any layer set to NA in all layers. This is useful for masking out areas where data are missing in any of the rasters.

Usage

## S4 method for signature 'GRaster'
na.omit(object, verbose = FALSE)

Arguments

object

A "stack" of GRasters.

verbose

Logical: If TRUE, display progress.

Value

A GRaster.

See Also

terra::na.omit(), stats::na.omit(), GRASS tool r.mapcalc (see grassHelp("r.mapcalc"))

Examples

if (grassStarted()) {

# Setup
library(terra)

### Mask layer-by-layer

# Elevation raster
madElev <- fastData("madElev")
madForest2000 <- fastData("madForest2000")

# Convert SpatRasters to GRasters:
elev <- fast(madElev)
forest2000 <- fast(madForest2000)

# Make a random layer and stack it with elevation:
x <- c(elev, forest2000)

# Put NAs in cells where any layer has an NA:
masked <- na.omit(x, verbose = TRUE)
plot(masked)

}

fasterRaster documentation built on Nov. 25, 2026, 9:08 a.m.