I want to aggregate a very fine resolution raster at various coarser resolutions (that represents many 1000s of categorical polygons).
The aggregate{raster} function is what i've traditionally used, the factor arguement reduces the resolution while the 'fun' allows me to sum, max or whatever.
However, when using fun=modal, how does aggregate behave? If I simply reduce the resolution by a factor of 2 (i.e. quarter the total number of cells) how does aggregate work with new multimodal cells?
example code:
ras <- raster(nrows=10, ncols=10,xmn=0, xmx=10, ymn=0, ymx=10)
ras[1:50] <- 1
ras[51:100] <- 2
x1 <- c(9,10,20,34,54,56,67,88,99)
ras[x1] <- NA
plot(ras)
grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted",lwd = 1)
ras2 <- aggregate(ras,fact=2,expand=FALSE,fun=modal,na.rm=T)
x11()
plot(ras2)
grid(nx = 10, ny = 10, col = "lightgray", lty = "dotted",lwd = 1)
Ideally, when reducing by a factor, if there is a multimodal result I'd like aggregate to randomly assign the new cell one of the modal values and not always choose the same (if that's indeed what it does). Larger factors such as 5 etc are less likely to cause this issue but it is still entirely possible.
FURTHERMORE: in the aggregate function above, na.rm is set to TRUE because i dont want every single NA cell to set the new, coarser, cell automatically to NA. However in the top right of the data, that new cell should really be NA as it has 3 smaller NA values. Do i just need to re-assign NA to 999 before running? and how will it work if there are 2 NA cells and 2 cells of another value?
Best Answer
That is not what it does. See
?modal
and theties
argument.Your question is really about the
modal
function which you pass on toaggregate
(both in packageraster
). So read the help file ofmodal
and pick the arguments you like to make it behave how you want it to. If you cannot do that, find a better one elsewhere, or write your own.The default behavior of
modal
is to break ties randomly, as illustrated here:You have a further question about
na.rm
stating thatI suppose you mean is that there are 3
NA
s and 11
such thatNA
should be the mode. Perhaps that should be allowed as an option, but currentlyNA
itself cannot be the modal value. The workaround you propose should be OK.