在R中用cbind组合相同大小的行

时间:2017-07-19 20:41:38

标签: r matrix cbind

我想按以下方式重新排列矩阵b:

> b <- matrix(1:24, 6, 4)
> b
      [,1] [,2] [,3] [,4]
[1,]    1    7   13   19
[2,]    2    8   14   20
[3,]    3    9   15   21
[4,]    4   10   16   22
[5,]    5   11   17   23
[6,]    6   12   18   24

> cbind(b[1:2, ], b[3:4, ], b[5:6, ])
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    1    7   13   19    3    9   15   21    5    11    17    23
[2,]    2    8   14   20    4   10   16   22    6    12    18    24

行数(此处为2)总是相同,但矩阵b将非常大,大小约为1M x 100.有一些快速的方法我可以做到这一点,没有环?感谢。

3 个答案:

答案 0 :(得分:3)

首先,使用blapply拆分成两行的块,然后使用cbind将子组划分为do.call

do.call(cbind, lapply(seq(1, NROW(b), 2), function(i) b[i:(i+1),]))
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,]    1    7   13   19    3    9   15   21    5    11    17    23
#[2,]    2    8   14   20    4   10   16   22    6    12    18    24

另一种方法是将奇数行和偶数行的元素分成两个向量,rbind将它们

rbind(do.call(c, data.frame(t(b[(1:NROW(b) %% 2) == 1,]))),
      do.call(c, data.frame(t(b[(1:NROW(b) %% 2) == 0,]))))
#     X11 X12 X13 X14 X21 X22 X23 X24 X31 X32 X33 X34
#[1,]   1   7  13  19   3   9  15  21   5  11  17  23
#[2,]   2   8  14  20   4  10  16  22   6  12  18  24

另一种方式,在很大程度上,类似于第二种方法

do.call(rbind, lapply(split(data.frame(b), 1:2), function(x) do.call(c, data.frame(t(x)))))
#  X11 X12 X13 X14 X31 X32 X33 X34 X51 X52 X53 X54
#1   1   7  13  19   3   9  15  21   5  11  17  23
#2   2   8  14  20   4  10  16  22   6  12  18  24

答案 1 :(得分:1)

您可以对数组进行整形,使用aperm对其进行转置并重新整形:

array(aperm(array(b, c(2,3,4)), c(1,3,2)), c(2,12))

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,]    1    7   13   19    3    9   15   21    5    11    17    23
#[2,]    2    8   14   20    4   10   16   22    6    12    18    24

另一种选择:

tb <- t(b)
rbind(c(tb[,c(T,F)]), c(tb[,c(F,T)]))
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,]    1    7   13   19    3    9   15   21    5    11    17    23
#[2,]    2    8   14   20    4   10   16   22    6    12    18    24

第一步,将其重塑为2, 3, 4

array(b, c(2,3,4))
, , 1

     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

     [,1] [,2] [,3]
[1,]   19   21   23
[2,]   20   22   24

第二步,切换轴/转置2和3:

aperm(array(b, c(2,3,4)), c(1,3,2))

, , 1

     [,1] [,2] [,3] [,4]
[1,]    1    7   13   19
[2,]    2    8   14   20

, , 2

     [,1] [,2] [,3] [,4]
[1,]    3    9   15   21
[2,]    4   10   16   22

, , 3

     [,1] [,2] [,3] [,4]
[1,]    5   11   17   23
[2,]    6   12   18   24

第三步,将其重塑为所需的尺寸:

array(aperm(array(b, dim = c(2,3,4)), c(1,3,2)), dim = c(2,12))
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    1    7   13   19    3    9   15   21    5    11    17    23
[2,]    2    8   14   20    4   10   16   22    6    12    18    24

答案 2 :(得分:1)

使用split对矩阵进行分块,然后循环添加维度cbind

# number of rows
n <- 2

do.call(cbind, lapply(split(b, (seq(nrow(b))-1) %/% n), matrix, nrow = n))

#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
# [1,]    1    7   13   19    3    9   15   21    5    11    17    23
# [2,]    2    8   14   20    4   10   16   22    6    12    18    24