title: assign values to empty row in R
body: <p>I have a function that returns a data frame with more than 50 columns (one row).
  This function is called inside a loop (lapply) so I get a list of dataframes  one row each.</p>
  
  <p>I initialize an empty dataframe at the very beginning in the function to be populated with the actual data returned by the process  (short version here for testing)</p>
  
  <pre><code>      df &lt;- read.csv(text = "conn,pc,server,profile")
  
  R&gt; df
  [1] conn    pc      server  profile car     id      amount 
  &lt;0 rows&gt; (or 0-length row.names)
  </code></pre>
  
  <p>I would like to assign values to the row taking into account that the process sometimes does not return values for all columns. </p>
  
  <p>Similar SO  questions tend to rely on initializing data frame this way:</p>
  
  <pre><code>df &lt;- data.frame("conn"= NA, "pc"= NA, "server"= NA, "profile" = NA)
  </code></pre>
  
  <p>but I don't want a row of NAs in case there's no value returned.</p>
  
  <p>So in my function I tried first:</p>
  
  <pre><code>returned.conn.value &lt;- 3  # assigned by hand for the sake of simplicity
  
  df$conn &lt;- returned.conn.value
      Error in `$&lt;-.data.frame`(`*tmp*`, conn, value = 3) : 
        replacement has 1 row, data has 0
  </code></pre>
  
  <p>So I discovered there's no way of adding value to a column inside an empty dataframe, but.....</p>
  
  <pre><code>df[1,1] &lt;- 3
  </code></pre>
  
  <p>This, to my surprise, worked. But, I consider very ugly to assign values to columns by index, so I tried this:</p>
  
  <pre><code>grep("^conn$", colnames(df))
  [1] 1
  </code></pre>
  
  <p>Uglier indeed, as it's forcing me to write the very same line for every column (instead of counting by hand, yes)</p>
  
  <p>So, I'm afraid I took a dead road leading to nowhere.</p>
  
  <p>is there any way of initialize an empty dataframe as it will allow me to do simple assignments as the one below with no issue?</p>
  
  <pre><code>df$conn &lt;- 3
  </code></pre>
  
  <p>Any hint will be much appreciated.</p>