Use do.call() instead of a loop when appending R data frames

Tip: When appending multiple data frames together, use do.call() 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 do.call()
my.list <- list()

#with a data frame, "missions":

#looping over missions by id...
for (m in 1:length(missions$mission_id)) {

miss.id <- missions$mission_id[m]
list.name <- as.character(miss.id)

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

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

}

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

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

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