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.
foo<-function(data, p, q, n){
results.frame <- data.frame()
for (i in 1:n){
for (i in p:q) {
run.model<-mark(data[sample(nrow(data), i),], model="Occupancy")
results<-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<-rbind(results.frame, results)
}
}
write.table(results.frame, "C:\\RWorkspace\\simulation_results.txt")
return(results.frame)
}