1
votes

Let's assume I have a raster representing land use classes in a certain resolution. I have to aggregate this raster with R to a coarser resolution and a modal value approach, in order to have the most dominating cell value in the coarser raster. This is easily achieved with

m <- aggregate(r, fact = 3, fun = modal, na.rm = TRUE)

However, I would like to weight the different land use classes – e.g. forest class (code 1) has a weight of 4 while water class (code 2) has a weight of 2 and street class has a weight of 1.

Is there a function that iterates through raster cells and applies a weight for each cell?

Thanks for any help?

1

1 Answers

1
votes

You could use reclassify to applying weights, but then what? Do you still want to compute the modal value after that?

I think what you want is your own function that you provide to aggregate. Perhaps something like this

library(raster)
f <- function(x, ...) {
    y <- c(
       rep(x[x==1], 4),
       rep(x[x==2], 2),
       x[x==3]
    )
    modal(y, ...)
}

r <- raster(res=5)
values(r) <- sample(c(1:3,2,3,3), ncell(r), replace=TRUE)

a <- aggregate(r, fact=10, fun=f)