我是R.的新手。我有以下代码
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)))
由于某种原因,我得到了行/ col的维度的NULL。
答案 0 :(得分:2)
' as.matrix'转动矢量' colMeans(x1)'到一个矩阵。然后尺寸为5和1,如预期的那样:
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
>
答案 1 :(得分:0)
如果您真的想将矢量强制转换为矩阵,可以尝试类似:
## Initial Computations.
sigma1 = matrix(c(2,0,0,1),2,2)
mu1 = matrix(c(0,10),2,1)
x1 = t(mvrnorm(n = 5, mu1, sigma1))
## Coerce the vector of colMeans into a 1 by 5 matrix.
colmeans.x1 = rbind(colMeans(x1))
## Inspect the dimensions of the matrix created above.
dim(colmeans.c1)
希望有帮助...