Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

mask

Mask values in a Raster object


Description

Create a new Raster* object that has the same values as x, except for the cells that are NA (or other maskvalue) in a 'mask'. These cells become NA (or other updatevalue). The mask can be either another Raster* object of the same extent and resolution, or a Spatial* object (e.g. SpatialPolygons) in which case all cells that are not covered by the Spatial object are set to updatevalue. You can use inverse=TRUE to set the cells that are not NA (or other maskvalue) in the mask, or not covered by the Spatial* object, to NA (or other updatvalue).

Usage

## S4 method for signature 'RasterLayer,RasterLayer'
mask(x, mask, filename="", inverse=FALSE, 
      maskvalue=NA, updatevalue=NA, updateNA=FALSE, ...)

## S4 method for signature 'RasterStackBrick,RasterLayer'
mask(x, mask, filename="", inverse=FALSE,
      maskvalue=NA, updatevalue=NA, updateNA=FALSE, ...)

## S4 method for signature 'RasterLayer,RasterStackBrick'
mask(x, mask, filename="", inverse=FALSE, 
      maskvalue=NA, updatevalue=NA, updateNA=FALSE, ...)

## S4 method for signature 'RasterStackBrick,RasterStackBrick'
mask(x, mask, filename="", inverse=FALSE, 
      maskvalue=NA, updatevalue=NA, updateNA=FALSE, ...)

## S4 method for signature 'Raster,Spatial'
mask(x, mask, filename="", inverse=FALSE, 
      updatevalue=NA, updateNA=FALSE, ...)

Arguments

x

Raster* object

mask

Raster* object or a Spatial* object

filename

character. Optional output filename

inverse

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

maskvalue

numeric. The value 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)

updateNA

logical. If TRUE, NA values outside the masked area are also updated to the the updatevalue (only relevant if the updatevalue is not NA

...

additional arguments as in writeRaster

Value

Raster* object

See Also

Examples

r <- raster(ncol=10, nrow=10)
m <- raster(ncol=10, nrow=10)
values(r) <- runif(ncell(r)) * 10
values(m) <- runif(ncell(r))
m[m < 0.5] <- NA
mr <- mask(r, m)

m2 <- m > .7
mr2 <- mask(r, m2, maskvalue=TRUE)

raster

Geographic Data Analysis and Modeling

v3.4-10
GPL (>= 3)
Authors
Robert J. Hijmans [cre, aut] (<https://orcid.org/0000-0001-5872-2872>), Jacob van Etten [ctb], Michael Sumner [ctb], Joe Cheng [ctb], Dan Baston [ctb], Andrew Bevan [ctb], Roger Bivand [ctb], Lorenzo Busetto [ctb], Mort Canty [ctb], Ben Fasoli [ctb], David Forrest [ctb], Aniruddha Ghosh [ctb], Duncan Golicher [ctb], Josh Gray [ctb], Jonathan A. Greenberg [ctb], Paul Hiemstra [ctb], Kassel Hingee [ctb], Institute for Mathematics Applied Geosciences [cph], Charles Karney [ctb], Matteo Mattiuzzi [ctb], Steven Mosher [ctb], Babak Naimi [ctb], Jakub Nowosad [ctb], Edzer Pebesma [ctb], Oscar Perpinan Lamigueiro [ctb], Etienne B. Racine [ctb], Barry Rowlingson [ctb], Ashton Shortridge [ctb], Bill Venables [ctb], Rafael Wueest [ctb]
Initial release
2021-05-02

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.