Skip to main content
4 of 4
added 8 characters in body
MarujoRe
  • 376
  • 2
  • 10
require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)

soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(raster::intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))

tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

MarujoRe
  • 376
  • 2
  • 10