简体   繁体   English

如何使用暗淡。 R 中数组的 rowVars 中的参数

[英]How to use dim. argument in rowVars on an array in R

This question is quite basic: I'm very confused by the documentation for rowVars in the package MatrixStats in R.这个问题非常基本:我对 R 中的 package MatrixStats 中的 rowVars 文档感到非常困惑。

I have an array of dimensions (12, 12, 10000), ie 10000 12x12 matrices.我有一个维度数组(12、12、10000),即 10000 个 12x12 矩阵。 rowMeans very easily gives me the mean of each row of each matrix in the form of a list with 10000 items. rowMeans 很容易以包含 10000 个项目的列表的形式为我提供每个矩阵的每一行的平均值。 I want to do the same with rowVars to get variances.我想对 rowVars 做同样的事情来获得差异。

This works fine for a single matrix, but for anything with more dimensions it gives an error message saying to use the dim argument, and I don't understand how it works.这适用于单个矩阵,但对于任何具有更多维度的东西,它会给出一条错误消息,说明使用 dim 参数,我不明白它是如何工作的。 The package documentation says that dim is "An integer vector of length two specifying the dimension of x, also when not a matrix." package 文档说 dim 是“一个长度为 2 的 integer 向量,指定 x 的维度,当不是矩阵时也是如此。” (where x is the object the function is to be used on). (其中 x 是 object function 将用于)。 However, I don't understand what this means, and haven't been able to find any helpful examples of it in use.但是,我不明白这意味着什么,也无法找到任何有用的使用示例。 What does 'specifying the dimension' mean- specifying how many dimensions? “指定维度”是什么意思 - 指定多少个维度? or specifying the size of each eg (12, 12, 10000)?或指定每个例如(12、12、10000)的大小? If so, how can it be of length 2?如果是这样,它的长度怎么可能是 2?

Thank you!谢谢!

The documentation for rowVars states that the input x should be a "numeric N x K matrix," so it sounds like this function only supports two-dimensional matrices. rowVars的文档指出输入x应该是“数字 N x K 矩阵”,所以听起来这个 function 只支持二维矩阵。 If you want the variances of each row for your 10000 matrices, you could instead do something like this:如果您想要 10000 个矩阵的每一行的方差,您可以改为执行以下操作:

mat <- array(rnorm(12*12*10000,0,1),dim=c(12,12,10000))
rvs <- sapply(1:10000,function(x) rowVars(mat[,,x]))

The resulting object rvs will be a matrix where the nth column is the row variances for the nth 12 x 12 matrix.生成的 object rvs将是一个矩阵,其中第 n 列是第 n 个 12 x 12 矩阵的行方差。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM