我有一个数据框left
,列name
和x
,我希望将缺少的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命令执行此操作吗?
答案 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
答案 2 :(得分:2)
这是一个可能的解决方案
master_opts => [-o => "StrictKeyChecking=no"]