title: How to facilitate nested for loops in R?
body: <p>I have nested for loops as below. I need p:q=1:300, and n=20. Function "mark" is the model of my interest(Package RMark). I know rbind can be slow but I have no idea what should be used to replace it. Otherwise what else I can do to make this function faster? Thanks.</p>
  
  <pre><code>foo&lt;-function(data, p, q, n){
  results.frame &lt;- data.frame()
  for (i in 1:n){
      for (i in p:q) {
          run.model&lt;-mark(data[sample(nrow(data), i),], model="Occupancy")       
          results&lt;-data.frame(summary(run.model)$real$p, Occupancy=summary(run.model)$real$Psi, se.p=t(as.matrix(summary(run.model, se=T)$real$p$se)), se.Psi=summary(run.model, se=T)$real$Psi$se, stations=i)
          results.frame&lt;-rbind(results.frame, results)
          } 
      }
  write.table(results.frame, "C:\\RWorkspace\\simulation_results.txt")
  return(results.frame)
  }
  </code></pre>