I need to create several raster mosaics. I'm using Package raster version 2.0-31 on a 64bits windows computer. I believe I did my homework checking through all possible blogs and asking this question to some colleagues, but still can't find a solution.
The problem I have is that I can't create a mosaic if my grids are listed in a raster object. I found this example that I though I could apply, but not, I get a weird error message. The example below represents my problem:
r <- raster()
r1 <- crop(r, extent(-10, 10, -10, 10))
r2 <- crop(r, extent(0, 20, 0, 20))
r3 <- crop(r, extent(10, 30, 10, 30))
r1[] <- 1:ncell(r1)
r2[] <- 1:ncell(r2)
r3[] <- 1:ncell(r3)
rasters1 <- list(r1, r2, r3)
mos <- mosaic(rasters1,fun=mean)
This is the error I get:
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function ‘mosaic’ for signature ‘"list", "missing"’
I also tried the function suggested in here, but didn't work either.
fmerge <- function(rasters1, fun, ...){
ex <- raster(union(rasters1))
res(ex) <- res(rasters1[[1]])
for( i in 1:length(rasters1) )
rasters[[i]] <- merge(rasters1[[i]], ex)
rasters <- stack(rasters1)
fun(rasters1, ...)
}
rfm <- fmerge(rasters1, mean, na.rm=T)
This is the error message:
Error in raster(union(rasters1)) :
error in evaluating the argument 'x' in selecting a method for function 'raster': Error in as.vector(y) : argument "y" is missing, with no default
See Question&Answers more detail:
os