我想将两个数据帧与来自两个不同来源的数据合并。来源A包括来自一些国家的数据,而来源B包括来源A的所有国家以及许多其他国家。请参阅这些数据框。
dfa<-data.frame("Country"=c("UK","USA","Canada"),"Value"=c(1,2,3),"Source"=c("A","A","A"),"Comments"=c("bla1","bla2","bla3"))
dfb<-data.frame("Country"=c("UK","USA","Canada","France","Germany"),"Value"=c(4,5,6,7,8),"Source"=c("B","B","B","B","B"),"Comments"=c("bla4","bla4","bla5","bla6","bla7"))
我的目标是生成合并后的数据框,其中包含数据框A(dfa
)中的行以及位于数据框B(dfb
)但不属于数据框B的国家/地区的行数据框A(dfa
)
答案 0 :(得分:3)
一种方式是:
rbind(dfa, dfb[!dfb$Country %in% dfa$Country, ])
答案 1 :(得分:2)
dplyr
的解决方案:
library(dplyr)
dfb %>%
anti_join(dfa, by = 'Country') %>%
rbind(dfa)
输出:
Country Value Source Comments
1 Germany 8 B bla7
2 France 7 B bla6
3 UK 1 A bla1
4 USA 2 A bla2
5 Canada 3 A bla3