Skip to content

Concatenating data frame

3 messages · Charles Cheung, jim holtman, Francisco J. Zagmutt

#
Hello, I have searched through the R-help archive and find that the easiest 
way to concatenate data records in a dataframe is to use rbind()


I know we can do that using rbind, but it is slow when we are doing rbind 
thousands of times to a growing list, each time adding one or two records to 
the ever growing existing data because in

existingRecords<-rbind(existingRecords, aNewRecordToBeAdded),

I am making a copy of the data each time rbind is called!

Is there a way to avoid these data copying?


Thank you in advance!

Charles
#
And adding to Jim's solution, you may be able to further improve the speed 
of your code by pre-allocating the list size i.e

result <- vector("list",100)
for (i in 1:100){
  result[[i]] <- data.frame(id=sample(letters,1), value=i)
}
newDataFrame <- do.call('rbind', result)

Cheers

Francisco