更新时间:2022-02-09 23:43:20
我会这样做:
library(data.table)
setDT(DF1); setDT(DF2)
DF1[DF2, x := ifelse(is.na(x), i.x, x), on=c("y","z")]
给出
x y z
1: 153 a 1
2: 163 b 1
3: 184 d 1
4: 123 a 2
5: 145 e 2
6: 176 c 2
7: 124 b 1
8: 199 a 2
评论.这种方法不是很好,因为它合并了DF1
的 whole ,而我们只需要合并is.na(x)
的子集.在这里,改进看起来像(感谢@Arun):
Comments. This approach isn't so great, since it merges the whole of DF1
, while we only need to merge the subset where is.na(x)
. Here, the improvement looks like (thanks, @Arun):
DF1[is.na(x), x := DF2[.SD, x, on=c("y", "z")]]
这种方式类似于@RHertel的答案.
This way is analogous to @RHertel's answer.