【问题标题】:Replace duplicate values from dataframe column using fuzzy match使用模糊匹配替换数据框列中的重复值
【发布时间】:2015-11-02 22:15:44
【问题描述】:

我正在尝试使用 library('RecordLinkage') 和 compare.dedup() 函数来替换单个列中的重复值。

和这个post类似,我有一个向量

tv3 = c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE", "TOURDE FRANZ", "GET FRESH") 

我想要的输出如下,基于设定的权重值(例如 > 0.8):

("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDEFRANCE", "TOURDEFRANCE", "GET FRESH") 

这是我尝试获取匹配数据框的代码:

tv3 = as.data.frame(c("TOURDEFRANCE", 'TOURDEFRANCE', "TOURDE FRANCE", 
                  "TOURDE FRANZ", "GET FRESH"))
colnames(tv3) <- "name"
tv3 %>% compare.dedup(strcmp = TRUE) %>%
    epiWeights() %>%
    epiClassify(0.8) %>%
    getPairs(show = "links", single.rows = TRUE) -> matches

但是为了得到我需要的东西,我使用了下面的循环:

matches <- matches[order(matches$id1),] 
tv3new <- tv3
for (i in 1:nrow(matches)) {
  tv3new[tv3new$name==matches[i,'name.2'],] <- matches[i,'name.1']
} 
tv3new

这给了我想要的东西,但想知道使用循环是否是最好的方法,或者我是否遗漏了一些明显的东西。

【问题讨论】:

    标签: r fuzzy-search data-cleaning


    【解决方案1】:

    没有循环:

    tv3new <- c(as.character(matches[tv3$name %in% matches$name.2*1, 2]), 
              as.character(tv3[!tv3$name %in% matches$name.2, ]))
    # If we need a data frame
    data.frame(name = tv3new)
    

    输出:

              name
    1 TOURDEFRANCE
    2 TOURDEFRANCE
    3 TOURDEFRANCE
    4 TOURDEFRANCE
    5    GET FRESH
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-14
      • 2017-01-06
      • 2020-09-10
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多