ungatoverde
ungatoverde

Reputation: 161

Merging data frames if value in a variable are different

I would like to merge two data frames with data from two different sources. The source A includes data from some countries, while the source B includes all countries of source A plus many others. See these data frames as example.

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"))

My goal is to produce in r a merged data frame that contains the rows from data frame A (dfa) plus those rows from countries that are located in data frame B (dfb) but not in data frame A (dfa)

Upvotes: 2

Views: 46

Answers (2)

LyzandeR
LyzandeR

Reputation: 37879

A solution with dplyr:

library(dplyr)
dfb %>%
  anti_join(dfa, by = 'Country') %>%
  rbind(dfa)

Out:

  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

Upvotes: 2

Tonio Liebrand
Tonio Liebrand

Reputation: 17689

One way would be this:

rbind(dfa, dfb[!dfb$Country %in% dfa$Country, ])

Upvotes: 3

Related Questions