I have two dataframes which i am trying to join based on a country name field and what i would like to achieve is the following: when a perfect match is found i would like to keep only that row, otherwise i would like to show all rows/options.
library(fuzzyjoin)
df1 <- data.frame(
country = c('Germany','Germany and Spain','Italy','Norway and Sweden','Austria','Spain'),
score = c(7,8,9,10,11,12)
)
df2 <- data.frame(
country_name = c('Germany and Spain','Germany','Germany.','Germania','Deutschland','Germany - ','Spun','Spain and Portugal','Italy','Italia','Greece and Italy',
'Australia','Austria...','Norway (Scandinavia)','Norway','Sweden'),
comments = c('xxx','rrr','ttt','hhhh','gggg','jjjj','uuuuu','ooooo','yyyyyyyyyy','bbbbb','llllll','wwwwwww','nnnnnnn','cc','mmmm','lllll')
)
j <- regex_left_join(df1,df2, by = c('country' = 'country_name'), ignore_case = T)
The results (j) show 'Germany and Spain' appear 3 times, the 1st occurrence is the perfect match, i would like to keep only this one and get rid of the other two. 'Norway and Sweden' has no perfect match so i would like to keep the two possible options/rows (as it is).
How can i do this?