Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I combine a list of similar dataframes into a single dataframe? [duplicate]

Tags:

list

dataframe

r

I have a dataframe:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)),              df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6))) 

Can I convert it to a single dataframe of the form:

data.frame(x = c('a', 'b', 'c', 'd', 'e', 'f'), y= c(1,2,3,4,5,6)) 

?

like image 967
David LeBauer Avatar asked Mar 03 '11 23:03

David LeBauer


People also ask

How do I combine multiple data frames into one?

The concat() function can be used to concatenate two Dataframes by adding the rows of one to the other. The merge() function is equivalent to the SQL JOIN clause. 'left', 'right' and 'inner' joins are all possible.

How do I combine a list of DataFrames into a single DataFrame in R?

To combine several data frames into one data frame, you can use the bind_rows() function from the dplyr package that adds the unique row names based on the names of list elements.

Can you merge multiple DataFrames in Pandas?

Pandas' merge and concat can be used to combine subsets of a DataFrame, or even data from different files. join function combines DataFrames based on index or column. Joining two DataFrames can be done in multiple ways (left, right, and inner) depending on what data must be in the final DataFrame.


2 Answers

do.call("rbind", foo) should do the trick.

like image 84
Hong Ooi Avatar answered Sep 28 '22 06:09

Hong Ooi


with plyr:

foo <- list(df1 = data.frame(x=c('a', 'b', 'c'),y = c(1,2,3)),          df2 = data.frame(x=c('d', 'e', 'f'),y = c(4,5,6)))  library(plyr) ldply(foo)[,-1]   x y 1 a 1 2 b 2 3 c 3 4 d 4 5 e 5 6 f 6 
like image 21
Sacha Epskamp Avatar answered Sep 28 '22 07:09

Sacha Epskamp