0

I have two datafiles. One of the files contains only one column with the name of the company (usually a hospital) and the other one contains a list of companies with the respective adresses. The problem is that the company names do not exactly match. How can i match them approximately ?

> dput(head(HOSPITALS[130:140,], 10))

I would like to obtain one datafile, where the company is matchen with an adress, if available in adress

Nneka
  • 1,764
  • 2
  • 15
  • 39

1 Answers1

1

Check out the fuzzyjoin package and the stringdist_join functions.

Here's a starting point. In your example data ignore_case = TRUE solves the matching problem. Depending on how the full data looks, you will have to experiment with the arguments (e.g. max_dist) and possibly filter the result until your achieve what you want.

library(dplyr)
library(fuzzyjoin)

HOSPITALS %>%
  stringdist_left_join(GH_MY,
                       by = c("hospital" = "hospital_name"),
                       ignore_case = TRUE,
                       max_dist = 2,
                       distance_col = "dist")

Result:

# A tibble: 10 x 6
   hospital             hospital_name    adress                 district   town      dist
   <chr>                <chr>            <chr>                  <chr>      <chr>    <dbl>
 1 HOSPITAL PAPAR       Hospital Papar   Peti Surat No. 6,      Papar      Sabah        0
 2 HOSPITAL PARIT BUNT~ Hospital Parit ~ Jalan Sempadan         Parit Bun~ Perak        0
 3 HOSPITAL PEKAN       Hospital Pekan   26600 Pekan            Pekan      Pahang       0
 4 HOSPITAL PENAWAR SD~ NA               NA                     NA         NA          NA
 5 HOSPITAL PORT DICKS~ Hospital Port D~ KM 11, Jalan Pantai    Port Dick~ Negeri ~     0
 6 HOSPITAL PULAU PINA~ Hospital Pulau ~ Jalan Residensi        Pulau Pin~ Pulau P~     0
 7 HOSPITAL PUSRAWI SD~ NA               NA                     NA         NA          NA
 8 HOSPITAL PUSRAWI SM~ NA               NA                     NA         NA          NA
 9 HOSPITAL PUTRAJAYA   Hospital Putraj~ Pusat Pentadbiran Ker~ Putrajaya  WP Putr~     0
10 HOSPITAL QUEEN ELIZ~ NA               NA                     NA         NA          NA
Aron Strandberg
  • 3,040
  • 9
  • 15