合并多个数据框

时间:2017-12-23 06:30:11

标签: 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"))

我想将它们合并到一个数据框中,使其看起来像

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

虽然我可以使用重复的dplyr :: full_join来做到这一点,但它太繁琐了。任何人都有更好的方法来做到这一点?

2 个答案:

答案 0 :(得分:5)

如果您有多个数据框,可以列出它们。 灵感来自vaettchen

Reduce(function(x,y) merge(x,y,by="name",all=TRUE) ,list(df1,df2,df3))

答案 1 :(得分:2)

within一样,有很多方法。对我来说,嵌套合并可以很好地完成这项工作:

ade4