【问题标题】:How to clean double lines from joint statement in R?如何从 R 中的联合声明中清除双线?
【发布时间】:2021-09-28 19:23:37
【问题描述】:

已完成连接操作以将地址与其自身进行比较。

library(tidyverse)
library(lubridate)
library(stringr)
library(stringdist)
library(fuzzyjoin)
doTheJoin <- function (threshold) {
      joined <- trimData(d_client_squashed) %>% 
        stringdist_left_join(
          trimData(d_client_squashed), 
          by = c(address_full="address_full"),
          distance_col = "distance",
          max_dist = threshold,
          method = "jw"
        )
    }    

d_client_squashed 的结构如下,包含字符串值:

Client_Reference adress_full
C01 Client1 Name, Street, Zipcode, Town
C02 Client2 Name, Street2, Zipcode2, Town2
... ...

以下操作:

sensible_matches <- doTheJoin(0.2)
View(sensible_matches %>% filter(Client_Reference.x != Client_Reference.y))

结果如下:

Client_Reference.x address_full.x Client_Reference.y address_full.y Distance
C01 Client1 Name, Street, Zipcode, Town C02 Client2 Name, Street2, Zipcode2, Town2 0.05486
C02 Client2 Name, Street2, Zipcode2, Town2 C01 Client1 Name, Street, Zipcode, Town 0.05486
... ... ... ... ...

此连接操作的输出是双重的,带有反向的客户端信息。距离值不是唯一的。如何对数据框进行子集化以避免那些双线?

【问题讨论】:

  • 你想要的输出是什么?你已经过滤了(Client_Reference.x != Client_Reference.y),所以你的样本是我所期望的。 `(Client_Reference.x == Client_Reference.y) 能给你想要的吗?
  • 总体目标是获取一个包含相似地址的列表,以便识别存在拼写错误或差异的重复条目,例如“Bachstr. 5”和“Bach Street 5”应该是相同的。使用 (Client_Reference.x != Client_Reference.y) 我想避免自己比较客户端地址。请求操作的目标是摆脱具有相同距离值但 Client_Reference.x 和 Client_Reference.y 只是倒置的第二行。因此,我想从列表中删除它的双重结果,以免对其进行两次验证。
  • 给每一行一个 rownumber .. 然后只在 rownumer.i > rownumber.j .. 的夫妇 (i,j) 上工作
  • 怎么样Client_Reference.x &gt; Client_Reference.y 而不是Client_Reference.x != Client_Reference.y
  • @Waldi 我真的很喜欢这种尝试。不幸的是,该解决方案无法在 Client_Reference.x 和 .y 上应用比较,因为这些都是因素。

标签: r left-join subset


【解决方案1】:

为了删除包含相同数据的行,可以根据包含的元素对它们进行排序,这样包含同一对Client_Reference的行之间没有区别,然后删除重复项。之后,您可以过滤包含与您相同的 Client_Reference 的那些。

sensible_matches <- sensible_matches[!duplicated(t(apply(sensible_matches,1,sort))),]
View(sensible_matches  %>% filter(Client_Reference.x != Client_Reference.y))

【讨论】:

  • 这完美解决了请求。谢谢!
猜你喜欢
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多