本文介绍了为什么昏暗的colMeans/rowMeans返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R的新手.我有以下代码

I am new to R. I have the following code

sigma1  = matrix(c(2,0,0,1),2,2)
mu1     = matrix(c(0,10),2,1)

x1      = t(mvrnorm(n = 5, mu1, sigma1))

print(rowMeans(x1))
print(dim(colMeans(x1)))

出于某种原因,我的行/列均值维数为NULL.

and for some reason I get NULL for dimension of row/col means.

推荐答案

'as.matrix'将向量'colMeans(x1)'转换为矩阵.然后尺寸为5和1,如预期:

'as.matrix' turns the vector 'colMeans(x1)' into a matrix. Then the dimensions are 5 and 1, as expected:

library(rockchalk)

sigma1  = matrix(c(2,0,0,1),2,2)
mu1     = matrix(c(0,10),2,1)
x1      = t(mvrnorm(n = 5, mu1, sigma1))

print(rowMeans(x1))
print(dim(colMeans(x1)))
print(dim(as.matrix(colMeans(x1))))

输出:

> print(rowMeans(x1))
[1] -0.1518187  9.4232239

> print(dim(colMeans(x1)))
NULL

> print(dim(as.matrix(colMeans(x1))))
[1] 5 1
>

这篇关于为什么昏暗的colMeans/rowMeans返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:45