left_join两个数据帧并覆盖 [英] left_join two data frames and overwrite
问题描述
我想合并两个数据框,其中 df2
会覆盖 NA
或 df1
。 合并数据框并覆盖值提供了数据。表
选项,但是我想知道是否有一种方法可以使用 dplyr
来做到这一点。我已经尝试了所有 _join
选项,但似乎都没有这样做。有没有办法用 dplyr
?
I'd like to merge two data frames where df2
overwrites any values that are NA
or present in df1
. Merge data frames and overwrite values provides a data.table
option, but I'd like to know if there is a way to do this with dplyr
. I've tried all of the _join
options but none seem to do this. Is there a way to do this with dplyr
?
这里是一个例子:
df1 <- data.frame(y = c("A", "B", "C", "D"), x1 = c(1,2,NA, 4))
df2 <- data.frame(y = c("A", "B", "C"), x1 = c(5, 6, 7))
所需的输出:
y x1
1 A 5
2 B 6
3 C 7
4 D 4
推荐答案
我认为您想要的是保持 df2
的值,仅添加 df1
中不存在的 df2
中没有的那些,这就是 anti_join
可以:
I think what you want is to keep the values of df2
and only add the ones in df1
that are not present in df2
which is what anti_join
does:
anti_join返回x中的所有行,其中y中没有匹配的值,仅保留x中的列。
"anti_join return all rows from x where there are not matching values in y, keeping just columns from x."
我的解决方案:
df3 <- anti_join(df1, df2, by = "y") %>% bind_rows(df2)
Warning messages:
1: In anti_join_impl(x, y, by$x, by$y) :
joining factors with different levels, coercing to character vector
2: In rbind_all(x, .id) : Unequal factor levels: coercing to character
> df3
Source: local data frame [4 x 2]
y x1
(chr) (dbl)
1 D 4
2 A 5
3 B 6
4 C 7
此行给出所需的输出(在顺序不同),但是,您应该注意警告消息,在使用数据集时,请确保将 y
读取为字符变量。
this line gives the desired output (in a different order) but, you should pay attention to the warning message, when working with your dataset be sure to read y
as a character variable.
这篇关于left_join两个数据帧并覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!