title: Add a column to empty data.frame
body: <p>I want to initialise a column in a data.frame look so:</p>
  
  <pre><code>df$newCol = 1
  </code></pre>
  
  <p>where df is a data.frame that I have defined earlier and already done some processing on.  As long as nrow(df)>0, this isn't a problem, but sometimes my data.frame has row length 0 and I get:</p>
  
  <pre><code>&gt; df$newCol  = 1
  Error in `[[&lt;-`(`*tmp*`, name, value = 1) :
   1 elements in value to replace 0 elements
  </code></pre>
  
  <p>I can work around this by changing my original line to</p>
  
  <pre><code>df$newCol = rep(1,nrow(df))
  </code></pre>
  
  <p>but this seems a bit clumsy and is computationally prohibitive if the number of rows in df is large.  Is there a built in or standard solution to this problem?  Or should I use some custom function like so</p>
  
  <pre><code>addCol = function(df,name,value) {
    if(nrow(df)==0){
      df[,name] = rep(value,0)
    }else{
      df[,name] = value
    }
    df
  }
  </code></pre>