Thursday, January 19, 2017

r - Convert a list of data frames into one data frame



I have code that at one place ends up with a list of data frames which I really want to convert to a single big data frame.



I got some pointers from an earlier question which was trying to do something similar but more complex.



Here's an example of what I am starting with (this is grossly simplified for illustration):




listOfDataFrames <- vector(mode = "list", length = 100)

for (i in 1:100) {
listOfDataFrames[[i]] <- data.frame(a=sample(letters, 500, rep=T),
b=rnorm(500), c=rnorm(500))
}


I am currently using this:




  df <- do.call("rbind", listOfDataFrames)

Answer



Use bind_rows() from the dplyr package:



bind_rows(list_of_dataframes, .id = "column_label")

No comments:

Post a Comment