处理 3d 数组:
ar3d <- array(floor(100 * runif(100 * 20 * 30)), dim = c(100, 20, 30))
我很困惑为什么
length(apply(ar3d, MARGIN = 1, FUN = "sd"))
# [1] 100
同时
dim(apply(ar3d, MARGIN = 1, FUN = "var"))
# [1] 900 100
如何尽快计算 20 * 30 值之间的方差?
非常感谢
最佳答案
对于 var
,如果您想获得标量,则应使用 var(c(x))
而不是 var(x)
相反,协方差矩阵。
当您输入 ?var
时,您会看到
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.
你可以尝试一下
> 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
关于r - R 中 3D 数组的方差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76651160/