title: Replace contents of two different sized data frames in R
body: <p>I have a data frame containing some data. For example, </p>
  
  <pre><code>a.df &lt;- data.frame(C = 5, D = 7)
  </code></pre>
  
  <p>I want to create another data frame with P columns and a single row of zeros, e.g.</p>
  
  <pre><code> b.df &lt;- data.frame(A = 0, B = 0, C = 0, D = 0, E = 0)
  </code></pre>
  
  <p>but the number of columns is computed, so the b.df data frame cannot be explicitly created as shown above. </p>
  
  <p>I then want to replace the values in the first (only) row of b.df with the values in the first row of the corresponding columns in a.df (in this case, b.df[1,”C”] = 5 and b.df[1,”D”] = 7, but other columns remain zero and the column names are preserved.</p>
  
  <p>Another solution would be to add zero columns to a.df, but in any event I need a data frame (or named vector) that has P elements with a single row with all zero elements except columns C and D.</p>
  
  <p>I have tried a number of approaches but I keep losing column names and run into other issues. Suggestions are much appreciated.</p>