-1

I am looking for help to insert values in one column of a database by comparing to another column in another database that has matching patterns of addresses. Please see below what I mean.

I am using R.

My data:

df1 <- structure(list(Names = c("A", "B", "C"), Address = c("101, 16 Ave NE", 
"203, 4 Cross SE", "115, 48 Ave SW")), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -3L))

  Names Address        
  <chr> <chr>          
1 A     101, 16 Ave NE 
2 B     203, 4 Cross SE
3 C     115, 48 Ave SW 


df2 <- structure(list(ID = c(1415, 2106, 2107), Address = c("101 16 Ave", 
"115 48 Ave SW Cresmont", "203 Skyview 4 Cross SE Near Walmart"
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-3L))

     ID Address                            
  <dbl> <chr>                              
1  1415 101 16 Ave                         
2  2106 115 48 Ave SW Cresmont             
3  2107 203 Skyview 4 Cross SE Near Walmart

Desired output:

  Names Address            ID
  <chr> <chr>           <dbl>
1 A     101, 16 Ave NE   1415
2 B     203, 4 Cross SE  2107
3 C     115, 48 Ave SW   2106

Dataframe Example

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Does this answer your question? [Match by argument pattern and left join](https://stackoverflow.com/questions/59057525/match-by-argument-pattern-and-left-join) – Michael Roswell Aug 17 '22 at 18:08

1 Answers1

0
Try this:
  
  We could join df1 df12 based on fuzzy string matching of their columns.

With max_dist we could define the Maximum distance to use for joining

See: ?stringdist_left_join

library(dplyr)
library(fuzzyjoin)

fuzzyjoin::stringdist_left_join(x=df1, y=df2, max_dist = .55, 
                                by=c('Address'='Address'), 
                                method = 'jaccard', 
                                distance_col = "dist") %>% 
  select(Names, Address = Address.x, ID)
# A tibble: 3 x 3
  Names Address            ID
  <chr> <chr>           <dbl>
1 A     101, 16 Ave NE   1415
2 B     203, 4 Cross SE  2107
3 C     115, 48 Ave SW   2106
TarJae
  • 72,363
  • 6
  • 19
  • 66