3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
這是使用該fuzzyjoin包裝的解決方案。它使用類似dplyr語法,并stringdist作為模糊匹配的可能類型之一。
如C8H10N4O2 所建議,stringdistmethod =“ jw”為您的示例創(chuàng)建最佳匹配。
作為建議由dgrtwo,fuzzyjoin的開發(fā)商,我用了一個(gè)大max_dist,然后使用dplyr::group_by和dplyr::top_n只得到最小距離的最佳匹配。
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) %>%
top_n(1, -dist)
#> # 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
- 3 回答
- 0 關(guān)注
- 1057 瀏覽
添加回答
舉報(bào)