我正在尝试使用包'RecordLinkage'中的levenshteinSim()函数相互比较字符串列表。但是,我很难弄清楚如何将我的字符串列表合并到函数中,因为它仅需要两个参数str1和str2。我正在尝试找到最佳方法,因为我的列表包含4k字符串。任何帮助深表感谢!
以下是一些示例数据:
sample <- c('apple', 'appeal', 'apparel', 'peel', 'peer', 'pear')
因此,我认为这可能是您想要的。该RecordLinkage
软件包不再在CRAN上,因此我去了另一个计算Levenshtein距离的软件包:
library(stringdist)
sample <- c('apple', 'appeal', 'apparel', 'peel', 'peer', 'pear')
df <- expand.grid(sample, sample) # this creates a dataframe of all combinations of the sample elements
stringdist(df$Var1, df$Var2, method = "lv")
输出:
[1] 0 3 3 4 4 4 3 0 3 3 4 3 3 3 0 4 5 4 4 3 4 0 1 2 4 4 5 1 0 1 4 3 4 2 1 0
也许更具吸引力-dplyr
版本:
library(dplyr)
df %>%
mutate(levenshtein = stringdist(Var1, Var2, method = "lv"))
哪个输出
Var1 Var2 levenshtein
1 apple apple 0
2 appeal apple 3
3 apparel apple 3
4 peel apple 4
5 peer apple 4
6 pear apple 4
...
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句