Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge multiple data frame [duplicate]

Tags:

r

df1 <- data.frame(name = c("A","B","C"), f1 = c(1,2,3), f2 = c("zz","mo","do"))
df2 <- data.frame(name = c("D","B","C"), f1 = c(1,4,6), f2 = c("ok","no","do"))
df3 <- data.frame(name = c("D","E","C"), f1 = c(1,2,3), f2 = c("so","yo","kl"))

I want to merge them into one data frame, so that it looks like

name    df.1f1   df1.f2    df2.f1   df2.f2   df3.f1   df3.f2
A            1       zz        na       na       na       na     
B            2       mo         4       no       na       na      
C            3       do         6       do        3       kl
D           na       na         1       ok        1       so
E           na       na        na       na        2       yo

Although I can use repeated dplyr::full_join to do this but it is too tedious. Anyone have a better way to do this?

like image 227
pill45 Avatar asked Mar 03 '26 19:03

pill45


1 Answers

In case you have multiple dataframes you can make a list of them. Inspired by vaettchen

Reduce(function(x,y) merge(x,y,by="name",all=TRUE) ,list(df1,df2,df3))
like image 92
PIG Avatar answered Mar 06 '26 09:03

PIG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!