R 如何将矩阵的所有列追加到一个矩阵中?

R 如何将矩阵的所有列追加到一个矩阵中?,r,R,我想将矩阵中的所有列附加到一个列中 模拟示例 # Sample data testmat <- matrix (data = seq(1:3), nrow = 3, ncol = 3) > testmat [,1] [,2] [,3] [1,] 1 1 1 [2,] 2 2 2 [3,] 3 3 3 我的第一个猜测是: onecol <- as.matrix (apply (X = testmat, MARGI

我想将矩阵中的所有列附加到一个列中

模拟示例

# Sample data
testmat <- matrix (data = seq(1:3), nrow = 3, ncol = 3)

> testmat
     [,1] [,2] [,3]
[1,]    1    1    1
[2,]    2    2    2
[3,]    3    3    3
我的第一个猜测是:

onecol <- as.matrix (apply (X = testmat, MARGIN = 2, FUN = cat))

onecol我们不需要在这里使用
apply
。一个
矩阵
是一个
向量
,具有
dim
属性,因此如果我们在原始
矩阵
上再次调用
矩阵
,并指定
ncol
,那么它将更改为一列
矩阵

matrix(testmat, ncol=1) 

或者我们可以更改原始
矩阵的
dim
(“testmat”)


dim(testmat)Ho,这比我想象的要容易得多!谢谢。你可以做
c(矩阵)
。我想即使是
matrix(testmat)
也可以。
matrix(testmat, ncol=1) 
dim(testmat) <- c(9, 1)