Search code examples
rstackrastercalc

Why do we use function calc and stack to do raster calculation? Is that better than using our desire function directly?


Recently, I need to calculate the mean of 12 world climate raster layers. There are two ways we can do. The first one is more direct:

mean.layer <- mean(L1, L2,......,L12) # L1 means the first layer

or

mean.layer <- (L1+L2+......+L12)/12

Another is new to me:

layer.stack <- stack(L1,L2,......,L12)
mean.layer <- calc(layer.stack, mean, na.rm = T)

Can someone explain the advantage of using calc and stack instead of using mean function directly? In my knowledge, we can manipulate the raster data in the same resolution and extension directly.


2021.7.10 edited. I rewrite the second method to correct some mistype.


Solution

  • Always please include some example data

    library(raster)
    b <- stack(system.file("external/rlogo.grd", package="raster"))
    

    These two statements are equivalent

    x <- mean(b)
    y <- calc(b, mean)
    

    But calc has a filename argument, so you can save the results to disk in one step.

    calc is especially advantageous when working with large rasters and more complex functions. For example

    z1 <- calc(b, function(i) 100 - sqrt(mean(i + 10)))
    

    Is equivalent to this

    z2 <- 100 - sqrt(mean(b + 10))
    

    But the latter may need to write 4 temp files to store the values, while the former would only need 1 such file.

    Your should avoid approaches to get the mean like this

     mean.layer <- (L1+L2+......+L12)/12
    

    It is cumbersome to write, prone to error, and does not scale (imagine doing this for 1200 rasters!).

    I do not know where you found the formulation with calc in your question, but that makes no sense, and does not work.