我正在尝试平均我的降水数据,它是一个光栅砖形式的对象(该对象称为“Prec”)。 “Prec”有 95 层。然而,我们的想法是仅对前 20 层进行平均。例如,第一层的第一个网格单元将与第二层的第一个网格单元进行平均,然后是第三层、第四层......一直到第20层。这将对每层的所有网格单元(每层 8192 个单元)执行此操作。这就是对象“Prec”的样子:
Prec
class : RasterBrick
dimensions : 64, 128, 8192, 95 (nrow, ncol, ncell, nlayers)
resolution : 2.8125, 2.789327 (x, y)
extent : -181.4062, 178.5938, -89.25846, 89.25846 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/Rain/Documents/My documents/All netCDF files/netcdffiles/MaxPrecIPSLIPSL-
CM5B-LRrcp85.nc
names : X1, X2, X3, X4, X5, X6, X7, X8, X9, X10, X11, X12, X13, X14, X15, ...
z-value : 1, 95 (min, max)
varname : onedaymax
我尝试了以下方法:
ncfname <- "MaxPrecIPSLIPSL-CM5B-LRrcp85.nc"
Prec <- brick(ncfname,var="onedaymax")
sm <- mean(Prec[1:20], na.rm=TRUE) #Attempting to calculate the mean by isolating first 20 layers
(example: Grid cell #1 of layer #1 is averaged with Grid cell #1 of layer #2....all the way to layer
#20
sm
[1] 20.8997
在此过程中,它仅返回一个值,而不是返回 20 层中的 8192 个平均值的单层。为什么会出现这种情况?
如有任何帮助,我们将不胜感激!
这是一个最小的、独立的、可重现的示例:
library(raster)
b <- brick(system.file("external/rlogo.grd", package="raster"))
获取所有(在本例中为 3)层的平均值
m <- mean(b)
获取前两层的平均值
m <- mean(b[[1:2]])
请注意,双括号
[[
用于子集层。单括号[
用于提取值。因此 b[1:2]
返回前两个单元格的值。参见?raster::subset