I have a number of raster layers contained in a single stack
as well as a SpatialPoints
object containing point coordinates. I am trying to change the value of the raster layers to NA if the cell contains a point.
I have provided a reproducible example below. However to put this in context, my real data consists of a 10 layers of 'habitat' (elevation, canopy cover, etc) that are all contained in a stack. The rasters
cover an area that is roughly 34 x 26 km. In addition I have ~10,000 GPS locations from animals.
So, for the reproducible example.
library(raster)
library(sp)
Make three rasters
and combine them into a stack
set.seed(123)
r1 <- raster(nrows=10, ncols=10)
r1 <- setValues(r1, sample(c(1:50), 100, replace = T))
r2 <- raster(nrows=10, ncols=10)
r2 <- setValues(r2, sample(c(40:50), 100, replace = T))
r3 <- raster(nrows=10, ncols=10)
r3 <- setValues(r3, sample(c(50:55), 100, replace = T))
Stack <- stack(r1, r2, r3)
nlayers(Stack)
Make the SpatialPoints
Pts <- SpatialPoints(data.frame(x = sample(-175:175, 50, replace = T),
y = sample(-75:75, 50, replace = T)))
Plot one of the rasters and the points
plot(Stack[[2]])
plot(Pts, add = T)
Now, is it possible to change the value of every cell that contains at least one point to NA? It would be great to perform this operation on the stack
.