Using R, I want to create 10 vectors (say 10x1) and concatenate their results into a matrix (say 10x10) one at a time.
使用R,我想创建10个向量(比如说10x1)并将它们的结果连接成一个矩阵(比如10x10)。
I figured that it might be most computationally-efficient to initialize an empty matrix first and then replace its values, column by column, with the vectors of interest.
我认为首先初始化空矩阵然后用感兴趣的矢量逐列替换它的值可能是计算效率最高的。
This is what I tried.
这就是我试过的。
# Initialize empty matrix
empty.m = matrix(nrow = 10, ncol = 10)
#Create vectors and attempt to insert them into matrix
for (i in 1:10) {
empty.m[, i] = sample(10)
}
My attempt to insert the output of sample into the matrix empty.m does not work, yielding the following error:
我尝试将样本输出插入矩阵empty.m不起作用,产生以下错误:
Error in matrix[, 1] = vec : object of type 'closure' is not subsettable
Do you have any suggestions for what I can do to accomplish what I want, avoiding this error?
你有什么建议可以做我想做的事情,避免这个错误吗?
1 个解决方案
#1
2
A much easier way to create the matrix is replicate
:
创建矩阵的更简单方法是复制:
replicate(10, sample(10))
The first 10
represents the number of columns.
前10个表示列数。
#1
2
A much easier way to create the matrix is replicate
:
创建矩阵的更简单方法是复制:
replicate(10, sample(10))
The first 10
represents the number of columns.
前10个表示列数。