Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将R中的单个列转换为矩阵_R_Vector_Matrix - Fatal编程技术网

如何将R中的单个列转换为矩阵

如何将R中的单个列转换为矩阵,r,vector,matrix,R,Vector,Matrix,我有一个长度为R*N的单列,我想将它转换为R中的RxN矩阵。有没有简单的方法来实现这一点而不使用循环和逐值赋值 格式是 r1 r2 r3 .. rR*N 将其转换为 r(1..N) r(N+1 .. 2*N) ... 这在R中非常简单。假设您的对象被称为dat: matrix(dat, R, byrow=TRUE) 其中R表示行数。如果数据集很大,可能需要节省内存。下面是一个简单的例子: x = round(runif(15, min=1, max=15)); ## use dim()

我有一个长度为R*N的单列,我想将它转换为R中的RxN矩阵。有没有简单的方法来实现这一点而不使用循环和逐值赋值

格式是

r1
r2
r3

..

rR*N
将其转换为

r(1..N)
r(N+1 .. 2*N)
...

这在R中非常简单。假设您的对象被称为
dat

matrix(dat, R, byrow=TRUE)

其中
R
表示行数。

如果数据集很大,可能需要节省内存。下面是一个简单的例子:

x = round(runif(15, min=1, max=15));
## use dim() to set dimensions instead of matrix() to avoid duplication of x
dim(x) <- c(3, 5);
rowNames = c("row1", "row2", "row3");
colNames = c("c1", "c2", "c3", "c4", "c5");
dimnames(x) = list(rowNames, colNames);
print(x);



     c1 c2 c3 c4 c5
row1  7  2  2 11  9
row2  2  6 11 14 10
row3  2 11  6 13 12

根据数据的结构,您可能需要添加
byrow=TRUE
> class(x)
[1] "matrix"