R - Vector/ Array Addition

前端 未结 2 876
一向
一向 2021-01-24 11:14

I a having a little trouble with vector or array operations.

I have three 3D arrays and i wanna find the average of them. How can i do that? we can\'t use mean()

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-24 11:25

    I'm not entirely sure what your desired output is, but I'm guessing that what you really want to build is not three 3D arrays, but one 4D array that you can then use apply on.

    Something like this:

    #Three 3D arrays...
    A <- array(runif(1:27),dim = c(3,3,3))
    B <- array(runif(1:27),dim = c(3,3,3))
    C <- array(runif(1:27),dim = c(3,3,3))
    
    #Become one 4D array
    D <- array(c(A,B,C),dim = c(3,3,3,3))
    
    #Now we can simply apply the function mean
    # and use it's na.rm = TRUE argument.
    apply(D,1:3,mean,na.rm = TRUE)
    

提交回复
热议问题