The netCDF4 file I want to work with in R is too large. I want to write a loop that will read in a chunk of the data and summarize it.
The variable I wish to read in has 4 dimensions; 'lat', 'lon', 'member' and 'time'. The time has a monthly resolution and the member contains 60 ensemble runs from a climate model.
Using the ncvar_get
command I have extracted 12 time slices from the netCDF, leaving me with a 4 dimension array.
num[1:144,1:69:1:60,1:12]
How would I aggregate this so that I would have annual data.
I am assuming that your fourth dimension, which has a length of 12 is the number of months and you would like to aggregate over this dimension and return an array of dimension c(144, 69, 60)
.
reproducible data (an array of the same dimensions, with all 1s)
myArray <- array(1, dim =c(144, 69, 60, 12))
Here is a method using apply
:
mySumArray <- apply(myArray, c(1,2,3), sum)
This returns an array with the following dimensions:
dim(mySumArray)
[1] 144 69 60
and where the first three elements are:
mySumArray[1:3]
[1] 12 12 12
If you wanted the mean or some other function, just replace sum
with you desired function.
An optimized version of summing and calculating the mean is rowSums
and rowMeans
.
mySumArray <- rowSums(myArray, dims=3)
returns the same result as above MUCH faster.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.