I am trying to create a new raster from one existing raster using conditional logic, ifelse
statements. The existing raster has numeric values and NA values. Here is what I tried so far, based on other posts within Stack Exchange:
f <- function(phos_all) {
ifelse(is.na(phos_all) | NA,
ifelse(phos_all<=4,30,
ifelse(phos_all>=4) & (phos_all<10), 20,
ifelse((phos_all>=10),0)))
}
phos_fert_2.5 <- calc(phos_all, fun = f, na.rm = TRUE)
When I run this code I get the following error:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function. Perhaps add '...' or 'na.rm' to the function arguments?
is.na(phos_all) | NA
? This is saying the same thing (NA == TRUE or NA) and there is no resulting condition. You can deal with elements that do not meet any condition by having the last condition in the nestedifelse
statments be NA, which would include existing NA values.