简体   繁体   中英

r - faster mean calculation in raster stack

I have a stack of some 400 rasters, for which I want to calculate the mean at each cell. I am using the function "mean" in the package raster, but it is taking too long. Is there an alternative function/package to calculate the mean of large stacks?

 # brief example
logo <- stack(system.file("external/rlogo.grd", package="raster")) 
logoMean=mean(logo)

As long as your calculations are cell by cell, you can use parallelization. The easiest access to parallel raster processing is via the clusterR function in the raster-package . This of course only makes sense with raster files sufficiently large enough so the overhead involved does not actually make processing slower.

library(raster)
logo <- stack(system.file("external/rlogo.grd", package="raster")) 
ncores <- 4 # define the number of cores you want to use
beginCluster(ncores)
logoMean <- clusterR(logo, mean, args=list(na.rm=TRUE))
endCluster()

logoMean_old <- mean(logo)
identical(logoMean, logoMean_old)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM