r - How to convert 4d array to 3d array subsetting on specific elements of one of the dimensions -



r - How to convert 4d array to 3d array subsetting on specific elements of one of the dimensions -

here easy question.. struggling help much appreciated.

i have 4d info wish transform 3d data. info has next attributes:

lon <- 1:96 lat <- 1:73 lev <- 1:60 tme <- 1:12 info <- array(runif(96*73*60*12), dim=c(96,73,60,12) ) # fill random test values

what calculate mean of first few levels (say 1:6). new info of form:

new.data <- array(96*73*12), dim=c(96,73,12) ) # 1 time again test info

but contain mean of first 5 levels of data. @ moment way have been able create work write rather inefficient loop extracts each of first 5 levels , divides sum of 5 mean.

i have tried:

new.data <- apply(data, c(1,2,4), mean)

which nicely gives me mean of vertical levels can't understand how subset 3rd dimension average of few! e.g.

new.data <- apply(data, c(1,2,3[1:5],4), mean) # returns error in ds[-margin] : 0's may mixed negative subscripts

i desperate help!

apply indexing (the proper utilize of "[") should plenty mean of first 6 levels of 3rd dimension if understand terminology:

> str(apply(data[,,1:6,] , c(1,2,4), fun=mean) ) num [1:96, 1:73, 1:12] 0.327 0.717 0.611 0.388 0.47 ...

this returns 96 x 73 12 matrix.

r multidimensional-array average

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -