[R-sig-Geo] Run focal function on a multi-layer raster

Thiago V. dos Santos thi_veloso at yahoo.com.br
Sat May 21 22:02:23 CEST 2016


I have a multi-layer raster, in this case a rasterbrick, on which I would like to apply a focal function on each layer, and get another rasterbrick as a result.

I am trying to use calc, but apparently I am not assembling my function in the proper way:

require(raster)
r <- raster(ncol=50, nrow=50)
r[]=1:ncell(r)
b <- brick(r,r,r,r,r,r)
b <- b * 1:6

myfocalfun <- function(x){
b.f <- focal(x, w=matrix(1, 5, 5), mean)
return(b.f)
}

b1 <- calc(b, myfocalfun)

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : 
cannot use this function

What am I missing here?
 Greetings,
 -- Thiago V. dos Santos

PhD student
Land and Atmospheric Science
University of Minnesota



More information about the R-sig-Geo mailing list