0

レーベンシュタイン距離を介して、特定の文字列に最適な n 個を見つけたいと思います。Rの関数が最小距離を与えることはわかっていadistますが、結果の数をたとえば 10 にスケーリングしようとしています。以下にいくつかのコードがあります。

name <- c("holiday inn", "geico", "zgf", "morton phillips")
address <- c("400 lafayette pl tupelo ms", "227 geico plaza chevy chase md", 
     "811 quincy st washington dc", "1911 1st st rockville md")

source1 <- data.frame(name, address)

name <- c("williams sonoma", "mamas bbq", "davis polk", "hop a long diner",
  "joes crag shack", "mike lowry place", "holiday inn", "zummer")

name2 <- c(NA, NA, NA, NA, NA, NA, "hi express", "zummer gunsul frasca")
address <- c("2 reads way new castle de", "248 w 4th st newark de",
     "1100 21st st nw washington dc", "1804 w 5th st wilmington de",
     "1208 kenwood parkway holdridge nb", "4203 ocean drive miami fl",
     "400 lafayette pl tupelo ms", "811 quincy st washington dc")
source2 <- data.frame(name, name2, address)

dist.mat.nm <- adist(source1$name, source2$name, partial = T, ignore.case = TRUE)

dist.mat.ad <- adist(source1$address.full, source2$address.full, partial = TRUE, ignore.case = TRUE)
dist.mat <- ifelse(is.na(dist.mat.nm), dist.mat.ad, dist.mat.nm)
dist.mat2 <- ifelse(is.na(dist.mat.ad), dist.mat.nm, dist.mat.ad)
which.match <- function(x, nm) return(nm[which(x == min(x))[1]])
which.index <- function(x, nm) return(which(x == min(x))[1])

source2.matches.name <- apply(dist.mat, 1, which.match, nm = source2$name)
source2.name.index <- apply(dist.mat, 1, which.index, nm = 
source2$names[source2.matches.name])

目的の結果は、を含むデータ フレームsource1$nameです。おそらくfromを使った何か?不明な点があればお知らせください。どんな助けでも大歓迎です。ありがとう。adistsource1$addresstop_ndplyr

4

1 に答える 1