I would start with your ifelse
statement. You have is.na(phos_all) | NA
stating the condition [NA == TRUE or NA]
which is redundant and can be dealt with at the end if the ifelse
. You also have some misplaced/unnecessary parenthesis.
Let's clarify the f
reclassification function a bit.
f <- function(x, ...) {
ifelse(x <= 4, 30,
ifelse(x >= 4 & x < 10, 20,
ifelse(x >= 10, 0, NA )))
}
Now we can test it on a vector to make sure it is yielding expected results.
y <- round(runif(100, 0, 20))
y[c(2,50,90)] <- NA
f(y)
We can now roll it into a raster example. Note that I did add a ...
argument into the f
function. This can sometimes help calc
and overlay
with passing the function.
library(raster)
r <- raster(nrow=100, ncol=100)
r[] <- round(runif(ncell(r), 0, 20))
( rrc <- calc(r, fun = f) )
unique(rrc[])