[R-sig-Geo] logical operation on raster stack

Tim Haering gnireaht at googlemail.com
Tue Nov 8 08:03:54 CET 2011


Dear list members,

has somebody experience in analysing raster* objects with logical functions?
Consider following example:
# define a logical function
bi <- function(x){ which(x==1) + 1 != which(x==2) & which(x==1) - 1 != 
which(x==2) }
# generate some example data
t1 <- c(8, 5, 7, 1, 4, 3, 2, 6, 9)
t2 <- c(9, 6, 7, 4, 5, 3, 1, 2, 8)
# function works correct
bi(t1)
bi(t2)

I would like to use this function on a raster stack, but don't know how. 
Can someone please help me?
# example data
r1 <- raster(nrows=10, ncols=10)
values(r1) <- rbinom(100, size = 5, prob=0.5)
r2 <- r1+1
r3 <- r1+2
r <- stack(r1, r2, r3)

# the calc function doesn't work
mycalc <- calc(r, fun=bi)

Thanks a lot.

TIM



More information about the R-sig-Geo mailing list