Skip to content

Instantly share code, notes, and snippets.

@romunov
Last active November 13, 2020 23:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romunov/c9d1ca1628cfbca873679835a90cc395 to your computer and use it in GitHub Desktop.
Save romunov/c9d1ca1628cfbca873679835a90cc395 to your computer and use it in GitHub Desktop.
calculating means for raster stacks, cell-wise
library(raster)
library(microbenchmark)
logo <- stack(system.file("external/rlogo.grd", package="raster"))
la <- as.array(logo)
microbenchmark(vanilla = mean(logo),
overlay = overlay(logo, fun = mean), times = 50, calc(logo, fun = mean),
rasterapply = {raster(apply(la, MARGIN = c(1,2), FUN = mean))},
norasterapply = {apply(la, MARGIN = c(1,2), FUN = mean)})
Unit: milliseconds
expr min lq mean median uq max neval cld
vanilla 19.60386 21.57217 23.4185 22.48069 23.87271 56.78282 50 a
overlay 188.79896 203.31245 220.2954 212.86435 227.59977 511.91230 50 d
calc 40.63554 42.48914 44.3292 43.67887 45.95958 52.97399 50 b
rasterapply 150.51631 160.30246 172.9886 171.31683 183.67126 231.59569 50 c
norasterapply 147.07012 157.48260 168.5057 166.99851 179.94597 217.33666 50 c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment