title: Slow data.frame row assignation
body: <p>I am working with RMongoDB and I need to fill an empty data.frame with the values of a query. The results are quite long, about 2 milion documents (rows). </p>
  
  <p>While I was doing performance tests, I found out that the time for writing the values to a row increases by the dimension of the data frame. Maybe it is a well known issue and I am the last one to notice it. </p>
  
  <p>Some code example:</p>
  
  <pre><code>set.seed(20140430)
  nreg &lt;- 2e3
  dfres &lt;- as.data.frame(matrix(rep(NA,nreg*7),nrow=nreg,ncol=7))
  system.time(dfres[1e3,] &lt;-  c(1:5,"a","b"))
  summary(replicate(10,system.time(dfres[sample(1:nreg,1),] &lt;- c(1:5,"a","b"))[3]))
  
  nreg &lt;- 2e6
  dfres &lt;- as.data.frame(matrix(rep(NA,nreg*7),nrow=nreg,ncol=7))
  system.time(dfres[1e3,] &lt;-  c(1:5,"a","b"))
  summary(replicate(10,system.time(dfres[sample(1:nreg,1),] &lt;- c(1:5,"a","b"))[3]))
  </code></pre>
  
  <p>On my machine, the assignment at the 2 milion rows data.frame takes about 0.4 seconds. This is a lot of time if I want to fill the whole dataset. Here goes a second simulation in order to draw the issue.</p>
  
  <pre><code>nreg &lt;- seq(2e1,2e7,length.out=10)
  te &lt;- NULL 
  for(i in nreg){
      dfres &lt;- as.data.frame(matrix(rep(NA,i*7),nrow=i,ncol=7))
      te &lt;- c(te,mean(replicate(10,{r &lt;- sample(1:i,1); system.time(dfres[r,] &lt;- c(1:5,"a","b"))[3]}) ) )
  }
  plot(nreg,te,xlab="Number of rows",ylab="Avg. time for 10 random assignments [sec]",type="o")
  #rm(nreg,dfres,te)
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/x50zw.png" alt="enter image description here"></p>
  
  <p><strong>Question</strong>: Why this happens? Is there a quicker way to fill the data.frame in memory?</p>