加入两个表但填充公共列

时间:2016-09-06 21:29:19

标签: r dplyr

我有一个数据框left,列namex,我希望将缺少的x条目与相应的name条目进行匹配数据框right

left = data.frame(
  name = c("Bob", "Billy", "Roger", "Anna", "Kathy"),
  x = c(NA, NA, NA, 4, 5)
)

# > left
#   name  x
# 1 Bob   NA
# 2 Billy NA
# 3 Roger NA
# 4 Anna   4
# 5 Kathy  5

right = data.frame(
  name = c("Bob", "Billy", "Roger"),
  x = c(1, 2, 3)
)

# > right
#   name  x
# 1 Bob   1
# 2 Billy 2
# 3 Roger 3

left_join给了我两列

> left_join(left, right, by="name")
   name x.x x.y
1 Bob    NA   1
2 Billy  NA   2
3 Roger  NA   3
4 Anna    4  NA
5 Kathy   5  NA

但我想要的是:

    name x 
1 Bob     1   
2 Billy   2   
3 Roger   3   
4 Anna    4  
5 Kathy   5  

我可以使用join命令执行此操作吗?

3 个答案:

答案 0 :(得分:4)

library(data.table)

setDT(left)[right, on = 'name', x := i.x]
left
#    name x
#1:   Bob 1
#2: Billy 2
#3: Roger 3
#4:  Anna 4
#5: Kathy 5

答案 1 :(得分:3)

另一个

library(dplyr)
left_join(left, right, "name") %>% 
  transmute(name, x=coalesce(x.x, x.y))
#    name x
# 1   Bob 1
# 2 Billy 2
# 3 Roger 3
# 4  Anna 4
# 5 Kathy 5

Description at MDN String#match

答案 2 :(得分:2)

这是一个可能的解决方案

master_opts => [-o => "StrictKeyChecking=no"]