Sliding window function in R
Does somebody know whether there is a sliding window method in R for 2d matrices and not just vectors. I need to apply median function to an image stored in matrix The function focal() in the excellent raster package is good for this. It takes several arguments beyond those shown in the example below, and can be used to specify a non-rectangular sliding window if that's needed. library(raster) ## Create some example data m <- matrix(1, ncol=10, nrow=10) diag(m) <- 2 r <- as(m, "RasterLayer") # Coerce matrix to RasterLayer object ## Apply a function that returns a single value when passed