在R中将87x2矩阵转换为29x6

时间:2018-10-25 01:03:36

标签: r matrix

假设我有一个维度为87x2的矩阵。如何在29x6

中转换为维度r
set.seed(1)
mat1 = matrix(runif(174), 87, 2)

我想在下面这样

> matrix(c(1:12), 6, 2)
     [,1] [,2]
[1,]    1    7
[2,]    2    8
[3,]    3    9
[4,]    4   10
[5,]    5   11
[6,]    6   12
> matrix(c(1:12), 2, 6)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    3    5    7    9   11
[2,]    2    4    6    8   10   12

谢谢。

1 个答案:

答案 0 :(得分:3)

您可以执行以下操作:

mat1 <- matrix(c(1:12), 6, 2)
matrix(mat1, nrow = 2, ncol = 6)
#     [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    3    5    7    9   11
#[2,]    2    4    6    8   10   12

或直接使用dim

设置尺寸
dim(mat1) <- c(2, 6)