f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
msk <- ifel(r < 400, NA, 1)
m <- mask(r, msk)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)[1,]
mv1 <- mask(r, v)
mv2 <- crop(r, v, mask=TRUE)
Run the code above in your browser using DataLab