I have four quite huge RasterStacks and would like to do some simple calculations on them. How can I speed up these calculations? I found this approach using overlay(), but the calculations still take very long.
My RasterStacks (s1,s2,s3,s4) have all the dimensions : 26, 76, 1976, 3805 (nrow, ncol, ncell, nlayers)
and my current code looks like this:
out <- overlay(s1,s2,s3,s4, fun = function(rs1,rs2,rs3,rs4) {return((rs1+rs2-rs3-rs4)*1e3)})
Any ideas?
EDIT: To produce an RasterStack (e.g., s1) you can call the following function:
create_stack <- function(num.col,num.row,num.lay){
r <- raster(matrix(runif(num.row*num.col,0,10), ncol=num.col, nrow=num.row),
xmn=0, xmx=num.col, ymn=0, ymx=num.row )
ll <- replicate(num.lay , r )
return(stack(ll))
}
library(raster)
s1 <- create_stack(76,26,3805)