38

我一直在研究一种基于不完美字符串(例如公司名称)连接两个数据集的方法。过去我必须匹配两个非常脏的列表,一个列表包含姓名和财务信息,另一个列表包含姓名和地址。两者都没有可匹配的唯一 ID!假设已经进行了清洁,并且可能存在错字和插入。

到目前为止,AGREP 是我发现的最接近的工具。我可以在 AGREP 包中使用 levenshtein 距离,它测量两个字符串之间的删除、插入和替换的数量。AGREP 将返回距离最小(最相似)的字符串。

但是,我一直无法将此命令从单个值应用到整个数据帧。我粗略地使用了一个 for 循环来重复 AGREP 函数,但必须有一个更简单的方法。

请参阅以下代码:

a<-data.frame(name=c('Ace Co','Bayes', 'asd', 'Bcy', 'Baes', 'Bays'),price=c(10,13,2,1,15,1))
b<-data.frame(name=c('Ace Co.','Bayes Inc.','asdf'),qty=c(9,99,10))

for (i in 1:6){
    a$x[i] = agrep(a$name[i], b$name, value = TRUE, max = list(del = 0.2, ins = 0.3, sub = 0.4))
    a$Y[i] = agrep(a$name[i], b$name, value = FALSE, max = list(del = 0.2, ins = 0.3, sub = 0.4))
}
4

7 回答 7

29

这是使用该fuzzyjoin软件包的解决方案。它使用dplyr-like 语法并stringdist作为可能的模糊匹配类型之一。

正如@C8H10N4O2所建议stringdist的那样, method="jw" 为您的示例创建了最佳匹配。

正如@dgrtwo 的开发者所建议fuzzyjoin的那样,我使用了一个大的max_dist然后使用dplyr::group_by并且dplyr::slice_min只获得最小距离的最佳匹配。(slice_min替换旧的top_n,如果原始顺序很重要且不按字母顺序排列,请使用mutate(rank = row_number(dist)) %>% filter(rank == 1)


a <- data.frame(name = c('Ace Co', 'Bayes', 'asd', 'Bcy', 'Baes', 'Bays'),
                price = c(10, 13, 2, 1, 15, 1))
b <- data.frame(name = c('Ace Co.', 'Bayes Inc.', 'asdf'),
                qty = c(9, 99, 10))

library(fuzzyjoin); library(dplyr);

stringdist_join(a, b, 
                by = "name",
                mode = "left",
                ignore_case = FALSE, 
                method = "jw", 
                max_dist = 99, 
                distance_col = "dist") %>%
  group_by(name.x) %>%
  slice_min(order_by = dist, n = 1)

#> # A tibble: 6 x 5
#> # Groups:   name.x [6]
#>   name.x price     name.y   qty       dist
#>   <fctr> <dbl>     <fctr> <dbl>      <dbl>
#> 1 Ace Co    10    Ace Co.     9 0.04761905
#> 2  Bayes    13 Bayes Inc.    99 0.16666667
#> 3    asd     2       asdf    10 0.08333333
#> 4    Bcy     1 Bayes Inc.    99 0.37777778
#> 5   Baes    15 Bayes Inc.    99 0.20000000
#> 6   Bays     1 Bayes Inc.    99 0.20000000
于 2017-06-06T04:01:52.423 回答
24

解决方案取决于您匹配a到的所需基数b。如果是一对一,您将获得上面三个最接近的匹配项。如果是多对一,您将获得六个。

一对一案例(需要分配算法):

当我不得不这样做之前,我将其视为具有距离矩阵和分配启发式的分配问题(下面使用贪婪分配)。如果您想要一个“最佳”解决方案,最好使用optim.

不熟悉 AGREP,但这里是stringdist用于您的距离矩阵的示例。

library(stringdist)
d <- expand.grid(a$name,b$name) # Distance matrix in long form
names(d) <- c("a_name","b_name")
d$dist <- stringdist(d$a_name,d$b_name, method="jw") # String edit distance (use your favorite function here)

# Greedy assignment heuristic (Your favorite heuristic here)
greedyAssign <- function(a,b,d){
  x <- numeric(length(a)) # assgn variable: 0 for unassigned but assignable, 
  # 1 for already assigned, -1 for unassigned and unassignable
  while(any(x==0)){
    min_d <- min(d[x==0]) # identify closest pair, arbitrarily selecting 1st if multiple pairs
    a_sel <- a[d==min_d & x==0][1] 
    b_sel <- b[d==min_d & a == a_sel & x==0][1] 
    x[a==a_sel & b == b_sel] <- 1
    x[x==0 & (a==a_sel|b==b_sel)] <- -1
  }
  cbind(a=a[x==1],b=b[x==1],d=d[x==1])
}
data.frame(greedyAssign(as.character(d$a_name),as.character(d$b_name),d$dist))

产生作业:

       a          b       d
1 Ace Co    Ace Co. 0.04762
2  Bayes Bayes Inc. 0.16667
3    asd       asdf 0.08333

我确信有一种更优雅的方式来执行贪婪的赋值启发式,但上面的方法对我有用。

多对一案例(不是分配问题):

do.call(rbind, unname(by(d, d$a_name, function(x) x[x$dist == min(x$dist),])))

产生结果:

   a_name     b_name    dist
1  Ace Co    Ace Co. 0.04762
11   Baes Bayes Inc. 0.20000
8   Bayes Bayes Inc. 0.16667
12   Bays Bayes Inc. 0.20000
10    Bcy Bayes Inc. 0.37778
15    asd       asdf 0.08333

编辑:用于method="jw"产生所需的结果。看help("stringdist-package")

于 2014-10-16T15:44:25.210 回答
3

我不确定这对您是否有用,约翰安德鲁斯,但它为您提供了另一种工具(来自RecordLinkage软件包)并且可能会有所帮助。

install.packages("ipred")
install.packages("evd")
install.packages("RSQLite")
install.packages("ff")
install.packages("ffbase")
install.packages("ada")
install.packages("~/RecordLinkage_0.4-1.tar.gz", repos = NULL, type = "source")

require(RecordLinkage) # it is not on CRAN so you must load source from Github, and there are 7 dependent packages, as per above

compareJW <- function(string, vec, cutoff) {
  require(RecordLinkage)
  jarowinkler(string, vec) > cutoff
}

a<-data.frame(name=c('Ace Co','Bayes', 'asd', 'Bcy', 'Baes', 'Bays'),price=c(10,13,2,1,15,1))
b<-data.frame(name=c('Ace Co.','Bayes Inc.','asdf'),qty=c(9,99,10))
a$name <- as.character(a$name)
b$name <- as.character(b$name)

test <- compareJW(string = a$name, vec = b$name, cutoff = 0.8)  # pick your level of cutoff, of course
data.frame(name = a$name, price = a$price, test = test)

> data.frame(name = a$name, price = a$price, test = test)
    name price  test
1 Ace Co    10  TRUE
2  Bayes    13  TRUE
3    asd     2  TRUE
4    Bcy     1 FALSE
5   Baes    15  TRUE
6   Bays     1 FALSE
于 2014-10-16T20:36:08.717 回答
1

同意上面的答案“不熟悉 AGREP,但这里是使用 stringdist 作为距离矩阵的示例。 ”但是从基于部分匹配数据元素的合并数据集中添加签名函数将更加准确,因为 LV 的计算基于关于位置/添加/删除

##Here's where the algorithm starts...
##I'm going to generate a signature from country names to reduce some of the minor differences between strings
##In this case, convert all characters to lower case, sort the words alphabetically, and then concatenate them with no spaces.
##So for example, United Kingdom would become kingdomunited
##We might also remove stopwords such as 'the' and 'of'.
signature=function(x){
  sig=paste(sort(unlist(strsplit(tolower(x)," "))),collapse='')
  return(sig)
}
于 2015-11-12T12:01:58.917 回答
1

模糊匹配

近似字符串匹配是将一个字符串与另一个字符串近似匹配。例如bananabananas
模糊匹配是在字符串中找到近似模式。例如bananabananas in pyjamas.

方法 R 实现
基本的 Bitap≈Levenshtein b$name <- lapply(b$name, agrep, a$name, value=TRUE); merge(a,b)
先进的 ?stringdist::stringdist-metrics fuzzyjoin::stringdist_join(a, b, mode='full', by=c('name'), method='lv')
模糊匹配 TRE agrep2 <- function(pattern, x) x[which.min(adist(pattern, x, partial=TRUE))]; b$name <- lapply(b$name, agrep2, a$name); merge(a, b)

自己跑

# Data
a <- data.frame(name=c('Ace Co.', 'Bayes Inc.', 'asdf'), qty=c(9,99,10))
b <- data.frame(name=c('Ace Company', 'Bayes', 'asd', 'Bcy', 'Baes', 'Bays'), price=c(10,13,2,1,15,1))

# Basic
c <- b
c$name.b <- c$name
c$name <- lapply(c$name, agrep, a$name, value=TRUE)
merge(a, c, all.x=TRUE)

# Advanced
fuzzyjoin::stringdist_join(a, b, mode='full')

# Fuzzy Match
c <- b
c$name.b <- c$name
c$name <- lapply(c$name, function(pattern, x) x[which.min(adist(pattern, x, partial=TRUE))], a$name)
merge(a, c)
于 2021-04-28T13:31:34.187 回答
1

lapply用于这些情况:

yournewvector: lapply(yourvector$yourvariable, agrep, yourothervector$yourothervariable, max.distance=0.01),

然后将其写为 csv 并不是那么简单:

write.csv(matrix(yournewvector, ncol=1), file="yournewvector.csv", row.names=FALSE)
于 2017-04-25T17:45:06.967 回答
-1

这是我用来获取公司出现在列表中的次数,尽管公司名称不完全匹配,

step.1安装语音包

step.2在“mylistofcompanynames”中创建一个名为“soundexcodes”的新列

step.3使用soundex函数返回“soundexcodes”中公司名称的soundex代码

step.4将公司名称和相应的 soundex 代码复制到一个名为“companysoundexcodestrainingfile”的新文件(2 列称为“companynames”和“soundexcode”)中

step.5删除“companysoundexcodestrainingfile”中 soundexcodes 的重复项

step.6浏览剩余公司名称列表并更改您希望它出现在原始公司中的名称

例如: Amazon Inc A625 可以是 Amazon A625 Accenture Limited A455 可以是 Accenture A455

step.6通过“soundexcodes”在 companysoundexcodestrainingfile$soundexcodes 和 mylistofcompanynames$soundexcodes 之间执行 left_join 或(简单的 vlookup)

step.7结果应该是原始列表,其中包含一个名为“co.y”的新列,其中包含您在培训文件中留下的公司名称。

step.8对“co.y”进行排序并检查大多数公司名称是否匹配正确,如果匹配,则将旧公司名称替换​​为由 soundex 代码的 vlookup 给出的新名称。

于 2018-01-24T08:50:01.360 回答