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))
?
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.
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.
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.
do.call("rbind", foo)
should do the trick.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With