I have two dataframes:
df1 <- data.frame(City=c("Munchen_Paris","Munchen_Paris","Barcelona_Milan", "Londen_Dublin","Madrid_Malaga"),
value1=c(11,21,33,2,53))
df2 <- data.frame(City=c("Munnich_Parijs","Barcelona_Munster","Barcelona_Milan","London_Dub","London_Oxford","Pisa_Luik"),
value2=c(22,2,44,54,29,65))
I try to merge these dataframes with fuzzyjoin.
The result I am looking for is:
City.x value1 City.y value2 string_distance
1 Munchen_Paris 11 Munnich_Parijs 22 5
2 Munchen_Paris 21 Munnich_Parijs 22 5
3 Barcelona_Milan 33 Barcelona_Milan 44 0
4 Londen_Dublin 2 London_Dub 54 4
(for every row in df1 with a match in df2 for City with a string_distance < 9, I want a row in the new table containing all columns from df1 and df2 with the lowest string_distance) When I do:
df3 <- stringdist_semi_join(df1, df2, by = "City", max_dist = 9, distance_col = "string_distance")
I receive only these columns:
> df3
City value1
1 Munchen_Paris 11
2 Munchen_Paris 21
3 Barcelona_Milan 33
4 Londen_Dublin 2
If I do a full join I receive this:
> df3 <- stringdist_full_join(df1, df2, by = "City", max_dist = 9, distance_col = "string_distance")
> df3
City.x value1 City.y value2 string_distance
1 Munchen_Paris 11 Munnich_Parijs 22 5
2 Munchen_Paris 21 Munnich_Parijs 22 5
3 Barcelona_Milan 33 Barcelona_Munster 2 6
4 Barcelona_Milan 33 Barcelona_Milan 44 0
5 Londen_Dublin 2 London_Dub 54 4
6 Londen_Dublin 2 London_Oxford 29 7
7 Madrid_Malaga 53 <NA> NA NA
8 <NA> NA Pisa_Luik 65 NA
I can delete the rows containing NA and group_by City.x although then I loose one of the first two rows.
If I do inner_join I receive this:
df3 <- stringdist_inner_join(df1, df2, by = "City", max_dist = 9, distance_col = "string_distance")
df3
> df3
City.x value1 City.y value2 string_distance
1 Munchen_Paris 11 Munnich_Parijs 22 5
2 Munchen_Paris 21 Munnich_Parijs 22 5
3 Barcelona_Milan 33 Barcelona_Munster 2 6
4 Barcelona_Milan 33 Barcelona_Milan 44 0
5 Londen_Dublin 2 London_Dub 54 4
6 Londen_Dublin 2 London_Oxford 29 7
Is it strange that stringdist_semi_join does not shows the columns of df2? Is there another way to reach the result I am looking for in the first table above?
Thanks a lot!