Reputation: 1004
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.
Upvotes: 2
Views: 205
Reputation: 101335
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
Upvotes: 1
Reputation: 8572
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.
Upvotes: 2