Use instead of a loop when appending R data frames

Tip: When appending multiple data frames together, use instead of looping. The trick is to store all of the data frames within a list object:

#make a list object that will include objects for
my.list <- list()

#with a data frame, "missions":

#looping over missions by id...
for (m in 1:length(missions$mission_id)) { <- missions$mission_id[m] <- as.character(

#[do lots of manipulation resulting in a data frame called]

  #add the data to the list object
  my.list[[]] <-


# append together data for all missions using rbind in <-, my.list)

Difference in runtime is dramatic. N.B. the data frames must all have the same number of columns and corresponding column types.

This entry was posted in data munging, R and tagged , . Bookmark the permalink.