如何基于公共列合并多个数据帧

时间:2015-10-08 11:08:55

标签: r

我有三个数据框,我想通过公共列名(Gene)

合并所有三个数据集
dt1 <- read.table(header = TRUE, text = "Gene chr log
Sall3    5     -1.5
TRIM5    5     -3.4
PDIA5    2    -2.3 
Nfatc1   2     -3.5",  stringsAsFactors = FALSE)

dt2 <- read.table(header = TRUE, text = "Gene       chr log
Sall3    5     -1.5
TRIM5    5     -3.4
PDIA5    2     -2.3 
DCC      2     -0.4",stringsAsFactors = FALSE)

dt3 <- read.table(header = TRUE, text = "Gene       chr log
Sall3    5     -1.5
TRIM5    5     -3.4
Ctdp1    2     -2.3,
Nfatc1   2      -4.5", stringsAsFactors = FALSE)

预期产出

dfinal <- read.table(header = TRUE, text = "Gene       chr log
    Sall3    5     -1.5
    TRIM5    5     -3.4", stringsAsFactors = FALSE)

1 个答案:

答案 0 :(得分:3)

您可以使用merge功能两次

merge(merge(dt1, dt2, by = "Gene"), dt3, by = "Gene")[1:3]

# Gene chr.x log.x
#1 Sall3     5  -1.5
#2 TRIM5     5  -3.4

正如@Pierre所提到的,对于多个数据帧,嵌套merge不是一个好选择。我们可以使用Reduce代替merge

Reduce(merge, list(dt1,dt2,dt3))

#   Gene chr  log
#1 Sall3   5 -1.5
#2 TRIM5   5 -3.4