【问题标题】:Fuzzy Matching/Join Two Data Frames of University Names [duplicate]模糊匹配/加入大学名称的两个数据框[重复]
【发布时间】:2018-10-30 19:53:47
【问题描述】:

我有一份包含拼写错误和不一致的大学名称列表。我需要将它们与大学名称的官方列表进行匹配,以将我的数据链接在一起。

我知道模糊匹配/加入是我要走的路,但我对正确的方法有点迷茫。任何帮助将不胜感激。

d<-data.frame(name=c("University of New Yorkk", "The University of South
 Carolina", "Syracuuse University", "University of South Texas", 
"The University of No Carolina"), score = c(1,3,6,10,4))

y<-data.frame(name=c("University of South Texas",  "The University of North
 Carolina", "University of South Carolina", "Syracuse
 University","University of New York"), distance = c(100, 400, 200, 20, 70))

而且我希望输出能够将它们尽可能紧密地合并在一起

matched<-data.frame(name=c("University of New Yorkk", "The University of South Carolina", 
"Syracuuse University","University of South Texas","The University of No Carolina"), 
correctmatch = c("University of New York", "University of South Carolina", 
"Syracuse University","University of South Texas", "The University of North Carolina"))

【问题讨论】:

    标签: r merge text-mining fuzzy fuzzyjoin


    【解决方案1】:

    我将adist() 用于此类事情,并且有一个名为closest_match() 的小包装函数来帮助将一个值与一组“良好/允许”值进行比较。

    library(magrittr) # for the %>%
    
    closest_match <- function(bad_value, good_values) {
      distances <- adist(bad_value, good_values, ignore.case = TRUE) %>%
        as.numeric() %>%
        setNames(good_values)
    
      distances[distances == min(distances)] %>%
        names()
    }
    
    sapply(d$name, function(x) closest_match(x, y$name)) %>%
      setNames(d$name)
    
    University of New Yorkk The University of South\n Carolina               Syracuuse University 
    "University of New York"     "University of South Carolina"           "University of New York" 
    University of South Texas      The University of No Carolina 
    "University of South Texas"     "University of South Carolina" 
    

    adist() 利用Levenshtein distance 比较两个字符串之间的相似度。

    【讨论】:

    • 我对这种格式很陌生,我有一个简单的问题。如何在您的最后一行代码中使用此矩阵输出?我将 sapply 行写入了一个新的数据框,并且有 na's。我是不是搞砸了?
    • 我会将最后一行保存为变量decoder,然后调用d$name &lt;- decoder[d$name] 用新的正确匹配项覆盖当前值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2023-04-03
    • 2021-01-26
    • 2021-10-23
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多