Averaging values across grid cells for each layer of a raster brick object
I am trying to average precipitation values across grid cells of a raster (which is masked to only account for land areas). This raster brick object, called "overlap.sub" has 138 layers (years). It was created as follows:
World_land <- readOGR("ne_110m_land.shp")
newprojection1 <- spTransform(World_land, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))overlap <- crop(RCP1pctCO2Median, extent(newprojection1))
overlap.sub <- mask(overlap, newprojection1) #This isolates grid cells on all land areas
The object "overlap.sub" has the following attributes:
class : RasterBrick
dimensions : 62, 127, 7874, 138 (nrow, ncol, ncell, nlayers)
resolution : 2.8125, 2.789327 (x, y)
extent : -178.5938, 178.5938, -89.25846, 83.67981 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : C:/Users/Travis/AppData/Local/Temp/Rtmp0GALJn/raster/r_tmp_2019-11-14_201408_9732_60407.grd
names : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6, layer.7, layer.8, layer.9, layer.10, layer.11, layer.12, layer.13, layer.14, layer.15, ...
min values : 0.42964514, 0.43375653, 0.51749371, 0.50838983, 0.45366730, 0.53099146, 0.49757186, 0.45697752, 0.41382199, 0.46082401, 0.45516687, 0.51857087, 0.41005131, 0.45956529, 0.47497867, ...
max values : 87.85833, 86.73710, 88.92577, 76.44161, 82.43909, 85.03188, 77.36673, 75.86527, 94.32226, 101.58247, 86.67574, 90.96802, 99.59785, 76.78394, 88.31423, ...
Now, to obtain the annual averages across the masked grid cells, so that I may plot a time series of the average precipitation across grid cells for Year 1, then for Year 2, then Year 3....all the way to Year 138 (essentially 138 averages):
Averageprec <- colMeans(overlap.sub)
However, this yields this error:
Error in colMeans(overlap.sub) :
'x' must be an array of at least two dimensions
I do believe that colMeans is the appropriate function for this, but why am I receiving this error? Unless there is another way to do this? Also, this would only average those grid cells that I masked (i.e. only the land areas), correct? Thanks, and any help/feedback would be greatly appreciated!!
[[alternative HTML version deleted]]