繁体   English   中英

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

[英]R fuzzy matching using agrep for two vectors

我想使用我正在使用 agrep 的模糊匹配来比较人们访问过的地方的 2 个向量。

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

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

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

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

不确定我是否正确理解您的问题。 但是一种迭代方法是使用for-loop或等效的*apply function,如下所示:

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

从这里我希望你能继续解决你问题的其余部分。

这是使用agrepl outer一个选项

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

这使

[1] 1 3 5

暂无
暂无

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

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