【问题标题】:R fuzzy matching using agrep for two vectors使用 agrep 对两个向量进行 R 模糊匹配
【发布时间】:2021-02-23 23:42:46
【问题描述】:

我想使用我正在使用 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&lt;-3,这样我就可以稍后在编码中使用它.

【问题讨论】:

    标签: r


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      这是一个使用outer + agrepl的选项

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

      给了

      [1] 1 3 5
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 2018-05-31
        • 2021-10-29
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多