r <- raster(ncols=36, nrows=18)
r[] <- 1:ncell(r)
# multiply values with 10
fun <- function(x) { x * 10 }
rc <- calc(r, fun)
# set values below 100 to NA.
fun <- function(x) { x[x<100] <- NA; return(x) }
rc <- calc(r, fun)
# set NA values to -9999
fun <- function(x) { x[is.na(x)] <- -9999; return(x)}
rc <- calc(rc, fun)
r2 <- r * 2
s <- stack(r, r2)
rs <- calc(s, sum)
Run the code above in your browser using DataLab