I´m trying to calculate moving majority values over an raster in R. The focal function in the raster package just provides mean, min and max. I have a raster with 3 values (1, 2 and 3) and I would like to have the value most abundant in a 3x3 window set in the center.
How can I do that most efficient in R? Thank you!
library(raster)
# create data
r <- raster(nrows = 120, ncol = 120, xmn=0)
r[] <- sample(3, ncell(r), replace=TRUE)