Skip to content

Set single raster to NA where values of raster stack are NA

3 messages · Robert J. Hijmans, Andrew Vitale

#
Andrew,

I think that a good approach might be, for your polygons:
p <- rbind(p1, p2, p3, etc, makeUniqueIDs=TRUE)
test <- mask(r4, p, inverse=TRUE)

# but to follow your example:
# solution 1
s2 <- stack(r1, r2, r3, r4)
test.a.cool <- calc(s2, function(x){ m <- apply(x, 1, function(z)
any(is.na(z))); m[m] <- NA; m})

# solution 2
test <- overlay(r4, s, fun=function(x, y){x[apply(y, 1,
function(z)any(is.na(z)))] <- NA; x})

# much simpler, solution 3
ss <- sum(s)
test <- mask(r4, ss)


Best, Robert
On Sun, Apr 28, 2013 at 9:23 PM, Andrew Vitale <vitale232 at gmail.com> wrote: