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