rrastercalc

Use calc function in r to calculate the mean from a stacked raster


I am using a stacked raster with the following properties:

class      : RasterStack 
dimensions : 116, 62, 7192, 100  (nrow, ncol, ncell, nlayers)
resolution : 200, 200  (x, y)
extent     : 501829.3, 514229.3, 3838920, 3862120  (xmin, xmax, ymin, ymax)
crs        : +proj=utm +zone=36 +datum=WGS84 +units=m +no_defs 
names      : Realization_1, Realization_2, Realization_3, Realization_4, Realization_5, Realization_6, Realization_7, Realization_8, Realization_9, Realization_10, Realization_11, Realization_12, Realization_13, Realization_14, Realization_15, ... 
min values :     0.2835853,     0.2561096,     0.3094338,     0.2750784,     0.3269123,     0.3061016,     0.3261848,     0.2906344,     0.3147314,      0.2518777,      0.3202900,      0.3056327,      0.3103129,      0.3044279,      0.2978966, ... 
max values :     0.8998597,     0.9295183,     0.8803385,     0.9196700,     0.8673874,     0.8853973,     0.8788281,     0.8999460,     0.8963305,      0.9357309,      0.8737690,      0.8818156,      0.8567144,      0.8692995,      0.8821410, ...

and I would like to calculate, at every pixel, the average value from all layers using the following command:

r_FI_mean   = raster::calc(rstack_FI,   fun = mean);

However, I get the following error:

Error: unable to find an inherited method for function ‘calc’ for signature ‘x = "RasterStack", fun = "numeric"’

when I use the same command to calculate the min or max value, no error message occurs.


Solution

  • No need to use calc. You can compute the mean of a RasterStack

    r_FI_mean   = mean(rstack_FI)