简体   繁体   English

使用 agrep 对两个向量进行 R 模糊匹配

[英]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.我想使用我正在使用 agrep 的模糊匹配来比较人们访问过的地方的 2 个向量。

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.如果我将 person1 手动去过的所有地方都输入到 agrep 中,那么它会告诉我人 1 访问了人 2 也访问过的 3 个地方。

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.我想要的是一种遍历人 1 访问的地方以得出结果“3”的方法,并将其分配给变量,例如person1result<-3 ,以便我稍后可以在编码中使用它。

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:但是一种迭代方法是使用for-loop或等效的*apply function,如下所示:

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这是使用agrepl outer一个选项

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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