title: Binding matrices in R with unequal rows
body: <p>I have two matrices with potentially both equal columns but unequal rows (but hopefully a solution will generalize to unequal numbers of both).</p>
  
  <p>I would like the following behavior (demonstrated using data.frames):</p>
  
  <pre><code>x = data.frame(z = c(8, 9), w = c(10, 11))
  y = data.frame(x = c(1, 2, 3), y = c(4, 5, 6))
  
  &gt; x
    z  w
  1 8 10
  2 9 11
  
  &gt; y
    x y
  1 1 4
  2 2 5
  3 3 6
  </code></pre>
  
  <p>And I would like to do something like</p>
  
  <pre><code>magic_cbind(x, y)
     z   w  x  y
  1  8  10  1  4
  2  9  11  2  5
  3 NA  NA  3  6
  </code></pre>
  
  <p>I found a perverse solution using <code>rbind.fill</code> from the <code>plyr</code> package:</p>
  
  <pre><code>&gt; x = data.frame(t(x))
  &gt; y = data.frame(t(y))
  &gt; x
    X1 X2
  z  8  9
  w 10 11
  &gt; y
    X1 X2 X3
  x  1  2  3
  y  4  5  6
  &gt; rbind.fill(x, y)
    X1 X2 X3
  1  8  9 NA
  2 10 11 NA
  3  1  2  3
  4  4  5  6
  &gt; rbind.fill(x, y) %&gt;% t %&gt;% as.matrix %&gt;% unname
       [,1] [,2] [,3] [,4]
  [1,]    8   10    1    4
  [2,]    9   11    2    5
  [3,]   NA   NA    3    6
  </code></pre>
  
  <p>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.</p>
  
  <p>I cobbled my solution up using the following questions:</p>
  
  <p><a href="https://stackoverflow.com/questions/23968018/bind-list-with-unequal-columns">Bind list with unequal columns</a></p>
  
  <p><a href="https://stackoverflow.com/questions/5531471/combining-cbind-vectors-of-different-length">Combining (cbind) vectors of different length</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26852820/r-column-binding-with-unequal-number-of-rows">R: column binding with unequal number of rows</a></p>