Assume I have four raster files: x, y, w and z and I want to create a new raster file based on the relationship of all of them (i.e., the hierarchical order of grid cells) using multiple nested conditionals as per function below:
x <- raster(nrows=100, ncols=100)
x[] <- runif(ncell(x), min = 0, max = 100)
y <- raster(nrows=100, ncols=100)
y[] <- runif(ncell(y), min = 0, max = 100)
w <- raster(nrows=100, ncols=100)
w[] <- runif(ncell(w), min = 0, max = 100)
z <- raster(nrows=100, ncols=100)
z[] <- runif(ncell(z), min = 0, max = 100)
My.fun <- function(x,y,z,w){
ifelse(x > y && x > w && x > z,
1,
ifelse(y > x && y > w && y > z,
2,
ifelse(w > x && w > y && w > z,
3,
ifelse(z > x && z > y && z > w,
4,NA),NA),NA),NA)
}
res <- overlay(x, y, z, w, fun = Vectorize(My.fun))
This is what I came up with so far. Yet, it does not seem to work. Wondering if anyone could give me a hand and shed some light on how I could sort it out?
Error message: "Error in (function (x, fun, filename = "", recycle = TRUE, forcefun = FALSE, : cannot use this formula, probably because it is not vectorized"
Many thanks.
Aucun commentaire:
Enregistrer un commentaire