rmatrixvardimensions

Variance over a 3D array in R


Working on a 3d array:

ar3d <- array(floor(100 * runif(100 * 20 * 30)), dim = c(100, 20, 30))

I am confused why

length(apply(ar3d, MARGIN = 1, FUN = "sd"))
# [1] 100

While

dim(apply(ar3d, MARGIN = 1, FUN = "var"))
# [1] 900 100

How can I calculate the variance amongst the 20 * 30 values as fast as possible?

Many thanks


Solution

  • For var, you should use var(c(x)) instead of var(x) if you want to obtain a scalar rather a covariance matrix.

    When you type ?var, you see

    var, cov and cor compute the variance of x and the covariance or correlation of x and y if these are vectors. If x and y are matrices then the covariances (or correlations) between the columns of x and the columns of y are computed.

    You can try it out like

    > ar3d <- array(floor(100 * runif(100 * 20 * 30)), dim = c(100, 20, 30))
    
    > length(apply(ar3d, 1, \(x) var(c(x))))
    [1] 100
    
    > length(apply(ar3d, 1, var))
    [1] 90000
    
    > length(apply(ar3d, 1, sd))
    [1] 100