r - R 中 3D 数组的方差

标签 r matrix var dimensions

处理 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/

相关文章:

list - 如何使用 R 自动创建结构列表?

r - ggplot : how to limit output in bar plot so only most frequent occurrences are shown?

python - 在ggplot中为不同的分类级别绘制不同的颜色

r - 如何将夏威夷和阿拉斯加添加到 R 中的空间多边形中?

android - 有没有办法将矩阵转换为替代比例?

MATLAB:如何在矩阵中设置颜色

python - 使用元组/列表作为数组索引 (Python)

ios - 无法获取另一个类中 var 的值

variables - 使用Kotlin的URL变量

c# - 锐锐器 : Visual Studio : warning use VAR