Name matching R

532 views Asked by At

I have 2 datasets with name. One with exact names and the other with exact and modified names

dt_t <- data.table(Name = list("Aaron RAMSEY", "Mesut OEZIL", "Sergio AGUERO"))
dt_f <- data.table(Name = list("Özil Mesut", "Ramsey Aaron", "Kun Agüero"))

I was thinking of making a table with dt_t in line and dt_f in column with the value of the jarowinkler function (which allows to calculate the similarity of a string) so that dt_f[i] is replaced by the dt_t[i] which has the highest jarowinkler value.

But I don't know how to do it, mutch less if it's possible.

Any idea are welcome

Thanks

1

There are 1 answers

1
ismirsehregal On

Here is a solution using adist:

library(data.table)

dt_t <- data.table(Name = list("Aaron RAMSEY", "Mesut OEZIL", "Sergio AGUERO"))
dt_f <- data.table(Name = list("Özil Mesut", "Ramsey Aaron", "Kun Agüero"))

string_dist <- adist(dt_t$Name, dt_f$Name, partial=TRUE, ignore.case=TRUE)

match_idx <- apply(string_dist, 2, which.min)

dt_match <- cbind(dt_t, dt_f[match_idx])

Edit ---------------------------------

Applying it row-wise:

library(data.table)

dt_t <- data.table(Name = (list("Aaron RAMSEY", "Mesut OEZIL", "Sergio AGUERO")))
dt_f <- data.table(Name = list("Özil Mesut", "Ramsey Aaron", "Kun Agüero"))

minDistMatch <- function(x, y){
  x <- as.list(x)
  y <- as.list(y)
  y[which.min(adist(x, y, partial=TRUE, ignore.case=TRUE))]
  }

dt_t[, Match := vapply(Name, minDistMatch, list(1L), dt_f$Name)]