Learn R Programming

terra (version 1.5-21)

mask: Mask values in a SpatRaster or SpatVector

Description

If x is a SpatRaster: Create a new SpatRaster that has the same values as SpatRaster x, except for the cells that are NA (or another maskvalue) in another SpatRaster (the 'mask'), or not covered by a SpatVector. These cells become NA (or another updatevalue).

If x is a SpatVector: Select geometries of x that intersect, or not intersect, with the geometries of y.

Usage

# S4 method for SpatRaster,SpatRaster
mask(x, mask, inverse=FALSE, maskvalues=NA, 
   updatevalue=NA, filename="", ...)
   
# S4 method for SpatRaster,SpatVector
mask(x, mask, inverse=FALSE, updatevalue=NA,
	touches=TRUE, filename="", ...)

# S4 method for SpatVector,SpatVector mask(x, mask, inverse=FALSE)

Arguments

x

SpatRaster or SpatVector

mask

SpatRaster or SpatVector

inverse

logical. If TRUE, areas on mask that are _not_ the maskvalue are masked

maskvalues

numeric. The value(s) in mask that indicates the cells of x that should become updatevalue (default = NA)

updatevalue

numeric. The value that cells of x should become if they are not covered by mask (and not NA)

touches

logical. If TRUE, all cells touched by lines or polygons will be masked, not just those on the line render path, or whose center point is within the polygon

filename

character. Output filename

...

additional arguments for writing files as in writeRaster

Value

SpatRaster

See Also

crop

Examples

Run this code
# NOT RUN {
r <- rast(ncols=10, nrows=10)
m <- rast(ncols=10, nrows=10)
values(r) <- 1:100
set.seed(1965)
x <- round(3 * runif(ncell(r)))
x[x==0] <- NA
values(m) <- x
mr <- mask(r, m)
# }

Run the code above in your browser using DataLab