R 对每行矩阵进行矢量化

R 对每行矩阵进行矢量化,r,vector,matrix,vectorization,R,Vector,Matrix,Vectorization,我想对矩阵进行矢量化,以获得“每行”的值。例如: mat = matrix(1:4,ncol=2) mat [,1] [,2] [1,] 1 3 [2,] 2 4 c(mat) # or as.vector(mat). Both give the values "per columns" [1] 1 2 3 4 我想得到这个: [1] 1 3 2 4 首先转置矩阵,例如c(t(mat))按行填充矩阵 matrix(1:4,ncol=2 , byrow=TR

我想对矩阵进行矢量化,以获得“每行”的值。例如:

mat = matrix(1:4,ncol=2)
mat
     [,1] [,2]
[1,]    1    3
[2,]    2    4

c(mat) # or as.vector(mat). Both give the values "per columns"
[1] 1 2 3 4
我想得到这个:

[1] 1 3 2 4

首先转置矩阵,例如
c(t(mat))

按行填充矩阵

matrix(1:4,ncol=2 , byrow=TRUE)  # matrix is filled by rows.

@shakthydoss有更好的方法可以做到这一点。查看示例/想法/方法。