【问题标题】:Average in three dimensional array every 12 step for third dimension三维数组中每 12 步的平均值
【发布时间】:2019-05-14 10:07:07
【问题描述】:

我有数组a

dim (a) 
[1] 126 59 240

我想每 12 步计算第三维的平均值。 我试过了:

rollapply(a,3,mean,by=12 )

但结果是长度为 11 的向量,仅此而已。

更好的是apply和具体的功能? 我的预期结果是一个带有dim 126 59 20的新数组

【问题讨论】:

    标签: arrays r average


    【解决方案1】:

    绝对不简单,但它确实有效。

    a = array(1:(126*59*240),c(126,59,240))          # Dummy data
    s = seq(1,240,by=12)                             # Sequence of cutpoints for 3rd dim
    aux =lapply(s,function(x){
      apply(a[,,x:(x+11)],c(1,2),mean,na.rm=TRUE)    # Get list with mean per each 12 3rd dim
    })
    
    aux2 = do.call(cbind,aux)                        # Bind all elements of list by column
    Y = array(aux2,dim=c(dim(aux[[1]]),length(aux))) # Reconvert into array
    
    > dim(Y)
    [1] 126  59  20
    

    【讨论】:

    • Tanks boski...一个问题:如果我尝试如何改变结果:“aux =lapply(s,function(x){ apply(a[,,x:(x+11)] ,3,mean,na.rm=TRUE) })" # 3 而不是 c(1,2) .... 为什么正确的 apply 参数是 c(1,2) 对于 3 dim 的数组?
    猜你喜欢
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    • 1970-01-01
    相关资源
    最近更新 更多