Adding data by row into an empty matrix and handling missing data
<p>I have an empty matrix with a certain number of columns that I'm trying to fill row-by-row with output vectors of a for-loop. However, some of the output are not the same length as the number of columns as my matrix, and just want to fill up those "empty spaces" with NAs.</p>

<p>For example:</p>

<pre><code>matrix.names &lt;- c("x1", "x2", "x3", "x4", "y1", "y2", "y3", "y4", "z1", "z2", "z3", "z4")
my.matrix &lt;- matrix(ncol = length(matrix.names))
colnames(my.matrix) &lt;- matrix.names
</code></pre>

<p>This would be the output from one iteration:  </p>

<pre><code>x &lt;- c(1,2)
y &lt;- c(4,2,1,5)
z &lt;- c(1)
</code></pre>

<p>Where I would want it in the matrix like this:</p>

<pre><code>      x1 x2  x3 x4 y1 y2 y3 y4 z1 z2 z3 z4
[1,]   1  2  NA NA  4  2  1  5  1 NA NA NA
</code></pre>

<p>The output from the next iteration would be, for example:</p>

<pre><code>x &lt;- c(1,1,1,1)
y &lt;- c(0,4)
z &lt;- c(4,1,3)
</code></pre>

<p>And added as a new row in the matrix:</p>

<pre><code>      x1 x2 x3 x4 y1 y2 y3 y4 z1 z2 z3 z4
[1,]   1  2 NA NA  4  2  1  5  1 NA NA NA
[2,]   1  1  1  1  0  4 NA NA  4  1  3 NA
</code></pre>

<p>It's not really a concern if I have a 0, it's just where there is no data. Also, the data is saved in such a way that whatever is there is listed in the row first, followed by NAs in empty slots. In other words, I'm not worried if an NA may pop up first.</p>

<p>Also, is such a thing better handled in data frames rather than matrices?</p>