0

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?

Romain
  • 171
  • 11

1 Answers1

1

You could use stringdist::stringdist to calculate the distance between matches and for entries where an exact match exists, keep only that:

library(dplyr)
j %>% 
  mutate(dist = stringdist::stringdist(country, country_name)) %>% # add distance
  group_by(country) %>%                                            # group entries
  mutate(exact = any(dist == 0)) %>%                               # check if exact match exists in group
  filter(!exact | dist == 0) %>%                                   # keep only entries where no exact match exists in the group OR where the entry is the exact match
  ungroup()
#> # A tibble: 5 x 6
#>   country           score country_name      comments    dist exact
#>   <chr>             <dbl> <chr>             <chr>      <dbl> <lgl>
#> 1 Germany               7 Germany           rrr            0 TRUE 
#> 2 Germany and Spain     8 Germany and Spain xxx            0 TRUE 
#> 3 Italy                 9 Italy             yyyyyyyyyy     0 TRUE 
#> 4 Norway and Sweden    10 Norway            mmmm          11 FALSE
#> 5 Norway and Sweden    10 Sweden            lllll         11 FALSE
JBGruber
  • 11,727
  • 1
  • 23
  • 45