简体   繁体   中英

R fuzzy matching using agrep for two vectors

I want to compare 2 vectors of where people have visited using fuzzy matching for which I am using agrep.

person1<-c("supermarket","garage","garden centre","restaurant")
person2<-c("supermkt","park","gdn center","gym","italian restaurant")

If I enter all of the places that person1 went to manually into agrep then it tells me that person 1 visited 3 places that person 2 also visited.

agrep("supermarket",person2,max.distance = 0.3)

What I want is a way to iterate through the places person 1 visited to come up with the result '3' and for this to be assigned to a variable eg person1result<-3 so I can then use this later on in the coding.

Not certain I'm understanding you question correctly. But one way to iterate would be using a for-loop or equivalently an *apply function as below:

sapply(person1, function(x)agrep(x, person2, max.distance = 0.3))
[1] 1 3 5

From here I hope you can continue to resolve the remaining part of your question.

Here is one option using outer + agrepl

which(
  outer(
    person1,
    person2,
    FUN = Vectorize(function(x, y) agrepl(x, y, max.distance = 0.3))
  ),
  arr.ind = TRUE
)[, "col"]

which gives

[1] 1 3 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM