I am trying to calculate the mean raster from a large RasterStack, with the following (using package raster):
raster_mean <- calc(raster_stack, conditonalmean)
However, I do not want a standard mean, but I wish to add some conditions to the computation. I have written the following function (where three conditions appear, one is to remove null values and the others are numerical):
conditonalmean <- function(x, na.rm){
v = var(x,na.rm = na.rm)
mean(v[v>=-10 & v<=10])
}
Which however returns the following error:
Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) :
cannot use this function
Any suggestion?