How do I group similar strings in R? [closed]

孤人 提交于 2019-12-04 18:47:16

There's a built in function called "adist" that computes a measure of distance between two words.

It's like using "agrep", except it returns the distance, instead of whether the words match according to some approximate matching criteria.

For the special case of words that can be interchanged with a comma(e.g. "hello,world" should be close to "world,hello"), here's a quick hack. You can modify the function pretty easily if you have other special cases.

adist_special <- function(word1, word2){
    min(adist(word1, word2),
        adist(word1, gsub(word2, 
                          pattern = "(.*),(.*)", 
                          repl="\\2,\\1")))
}

adist("hello,world", "world,hello")

 # 8
adist_special("hello,world", "world,hello")

 # 0
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!