RasterStack 的 mean(x) 和 calc(x, fun=mean) 有什么区别?

What is the difference between mean(x) and calc(x, fun=mean) for RasterStack?

我有大约 80 个光栅堆栈,每个栅格堆栈有 24 个层。我正在尝试计算每个堆栈的所有层的平均值(例如,在 24 层中按像素计算平均值)。

我找到了两种方法来实现这一点,但我得到的最小-最大值结果略有不同。这是一个例子:

library(raster)
set.seed(9999)

##create example raster list
rasterls=list()
for (i in 1:24){
  r <- raster(nrow=(255*5), ncol=(487*5), vals=runif(255*5*487*5))
  rasterls[[i]] <- r
}
##create raster stack
stackRas <- stack(rasterls)

##calculate mean using mean()
mean(stackRas, na.omit=T)

'''class      : RasterLayer 
dimensions : 1275, 2435, 3104625  (nrow, ncol, ncell)
resolution : 0.1478439, 0.1411765  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : memory
names      : layer 
values     : 0.2453108, 0.8256463  (min, max)
'''

##calculate mean using calc(fun=mean)
calc(stackRas, fun = mean, na.omit=T)

'''
class      : RasterLayer 
dimensions : 1275, 2435, 3104625  (nrow, ncol, ncell)
resolution : 0.1478439, 0.1411765  (x, y)
extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs 
source     : C:/Users/--/AppData/Local/Temp/RtmpoVVABX/raster/r_tmp_2021-04-28_120400_4844_87419.grd 
names      : layer
values     : 0.2138654, 0.8183816  (min, max)
'''

这两种方法有什么区别?为什么不同的最小值 - 最大值?我错过了什么吗?我应该使用哪一个?我很感激任何见解。谢谢

mean 没有 na.omit 参数。正确的参数应该是na.rm。当您为 na.omit 传递一个值时,它会被忽略,并且这两种方法在默认处理缺失值方面有所不同。如果我们使用正确的参数,结果是一样的。

mean(stackRas, na.rm=T)
# class      : RasterLayer 
# dimensions : 1275, 2435, 3104625  (nrow, ncol, ncell)
# resolution : 0.1478439, 0.1411765  (x, y)
# extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
# crs        : +proj=longlat +datum=WGS84 +no_defs 
# source     : memory
# names      : layer 
# values     : 0.2113316, 0.7704163  (min, max)

calc(stackRas, mean, na.rm=T)
# class      : RasterLayer 
# dimensions : 1275, 2435, 3104625  (nrow, ncol, ncell)
# resolution : 0.1478439, 0.1411765  (x, y)
# extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
# crs        : +proj=longlat +datum=WGS84 +no_defs 
# source     : /tmp/RtmpRMo1uo/raster/r_tmp_2021-04-28_124738_12579_61131.grd 
# names      : layer 
# values     : 0.2113316, 0.7704163  (min, max)