Skip to main content
added 5 characters in body
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

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[])

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[])

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[])
Source Link
Jeffrey Evans
  • 32k
  • 2
  • 48
  • 97

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[])