R中的矩阵绑定不等行(Binding matrices in R with unequal rows)

我有两个矩阵,可能两个列相等,但行数不等(但希望解决方案可以推广到两个数量不等)。

我想要以下行为(使用data.frames演示):

x = data.frame(z = c(8, 9), w = c(10, 11)) y = data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) > x z w 1 8 10 2 9 11 > y x y 1 1 4 2 2 5 3 3 6

我想做点什么

magic_cbind(x, y) z w x y 1 8 10 1 4 2 9 11 2 5 3 NA NA 3 6

我使用plyr包中的plyr找到了一个反常的解决方案:

> x = data.frame(t(x)) > y = data.frame(t(y)) > x X1 X2 z 8 9 w 10 11 > y X1 X2 X3 x 1 2 3 y 4 5 6 > rbind.fill(x, y) X1 X2 X3 1 8 9 NA 2 10 11 NA 3 1 2 3 4 4 5 6 > rbind.fill(x, y) %>% t %>% as.matrix %>% unname [,1] [,2] [,3] [,4] [1,] 8 10 1 4 [2,] 9 11 2 5 [3,] NA NA 3 6

但我想知道是否有更优雅的解决方案? 我事先并不知道矩阵的最终大小,这是一个问题,并且它在一个循环中增长(这是一种可怕的练习,但它不会变得足够大,实际上是一个问题)。 也就是说,给定一个矩阵,我试图以上述方式将通过循环获得的其他列绑定到它。

我使用以下问题拼凑了我的解决方案:

使用不相等的列绑定列表

组合不同长度的(cbind)向量

R:具有不等行数的列绑定

I have two matrices with potentially both equal columns but unequal rows (but hopefully a solution will generalize to unequal numbers of both).

I would like the following behavior (demonstrated using data.frames):

x = data.frame(z = c(8, 9), w = c(10, 11)) y = data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) > x z w 1 8 10 2 9 11 > y x y 1 1 4 2 2 5 3 3 6

And I would like to do something like

magic_cbind(x, y) z w x y 1 8 10 1 4 2 9 11 2 5 3 NA NA 3 6

I found a perverse solution using rbind.fill from the plyr package:

> x = data.frame(t(x)) > y = data.frame(t(y)) > x X1 X2 z 8 9 w 10 11 > y X1 X2 X3 x 1 2 3 y 4 5 6 > rbind.fill(x, y) X1 X2 X3 1 8 9 NA 2 10 11 NA 3 1 2 3 4 4 5 6 > rbind.fill(x, y) %>% t %>% as.matrix %>% unname [,1] [,2] [,3] [,4] [1,] 8 10 1 4 [2,] 9 11 2 5 [3,] NA NA 3 6

But I was wondering if there were a more elegant solution? I don't know the final size of the matrix in advance, which is a problem, and it grows inside a loop (which is terrible practice but it but won't grow large enough to actually be a concern). That is, given a matrix, I'm trying to bind additional columns obtained through a loop to it in the way described above.

I cobbled my solution up using the following questions:

Bind list with unequal columns

Combining (cbind) vectors of different length

R: column binding with unequal number of rows

最满意答案

我们可以使用来自rowr

rowr::cbind.fill(x, y, fill = NA) # z w x y #1 8 10 1 4 #2 9 11 2 5 #3 NA NA 3 6

We can use cbind.fill from rowr

rowr::cbind.fill(x, y, fill = NA) # z w x y #1 8 10 1 4 #2 9 11 2 5 #3 NA NA 3 6R中的矩阵绑定不等行(Binding matrices in R with unequal rows)

我有两个矩阵,可能两个列相等,但行数不等(但希望解决方案可以推广到两个数量不等)。

我想要以下行为(使用data.frames演示):

x = data.frame(z = c(8, 9), w = c(10, 11)) y = data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) > x z w 1 8 10 2 9 11 > y x y 1 1 4 2 2 5 3 3 6

我想做点什么

magic_cbind(x, y) z w x y 1 8 10 1 4 2 9 11 2 5 3 NA NA 3 6

我使用plyr包中的plyr找到了一个反常的解决方案:

> x = data.frame(t(x)) > y = data.frame(t(y)) > x X1 X2 z 8 9 w 10 11 > y X1 X2 X3 x 1 2 3 y 4 5 6 > rbind.fill(x, y) X1 X2 X3 1 8 9 NA 2 10 11 NA 3 1 2 3 4 4 5 6 > rbind.fill(x, y) %>% t %>% as.matrix %>% unname [,1] [,2] [,3] [,4] [1,] 8 10 1 4 [2,] 9 11 2 5 [3,] NA NA 3 6

但我想知道是否有更优雅的解决方案? 我事先并不知道矩阵的最终大小,这是一个问题,并且它在一个循环中增长(这是一种可怕的练习,但它不会变得足够大,实际上是一个问题)。 也就是说,给定一个矩阵,我试图以上述方式将通过循环获得的其他列绑定到它。

我使用以下问题拼凑了我的解决方案:

使用不相等的列绑定列表

组合不同长度的(cbind)向量

R:具有不等行数的列绑定

I have two matrices with potentially both equal columns but unequal rows (but hopefully a solution will generalize to unequal numbers of both).

I would like the following behavior (demonstrated using data.frames):

x = data.frame(z = c(8, 9), w = c(10, 11)) y = data.frame(x = c(1, 2, 3), y = c(4, 5, 6)) > x z w 1 8 10 2 9 11 > y x y 1 1 4 2 2 5 3 3 6

And I would like to do something like

magic_cbind(x, y) z w x y 1 8 10 1 4 2 9 11 2 5 3 NA NA 3 6

I found a perverse solution using rbind.fill from the plyr package:

> x = data.frame(t(x)) > y = data.frame(t(y)) > x X1 X2 z 8 9 w 10 11 > y X1 X2 X3 x 1 2 3 y 4 5 6 > rbind.fill(x, y) X1 X2 X3 1 8 9 NA 2 10 11 NA 3 1 2 3 4 4 5 6 > rbind.fill(x, y) %>% t %>% as.matrix %>% unname [,1] [,2] [,3] [,4] [1,] 8 10 1 4 [2,] 9 11 2 5 [3,] NA NA 3 6

But I was wondering if there were a more elegant solution? I don't know the final size of the matrix in advance, which is a problem, and it grows inside a loop (which is terrible practice but it but won't grow large enough to actually be a concern). That is, given a matrix, I'm trying to bind additional columns obtained through a loop to it in the way described above.

I cobbled my solution up using the following questions:

Bind list with unequal columns

Combining (cbind) vectors of different length

R: column binding with unequal number of rows

最满意答案

我们可以使用来自rowr

rowr::cbind.fill(x, y, fill = NA) # z w x y #1 8 10 1 4 #2 9 11 2 5 #3 NA NA 3 6

We can use cbind.fill from rowr

rowr::cbind.fill(x, y, fill = NA) # z w x y #1 8 10 1 4 #2 9 11 2 5 #3 NA NA 3 6