Maintaining headers in edit distance

50 views Asked by At

I am running edit distance using stringdist. The output replaces the input with a numbered list instead of the actual string being compared. This is currently what I have:

library(stringdist)

a <- c("foo", "bar", "bear", "boat", method = "lv")
stringdistmatrix(a)

1 2 3
2 3    
3 4 1  
4 3 2 2

I would like the output to look like the following so that I can see where the edit distance comes from.

foo bar bear
bar 3    
bear 4 1  
boat 3 2 2
2

There are 2 answers

0
Psidom On BEST ANSWER

There is the useNames parameter you can specify:

stringdistmatrix(a, useNames = TRUE)

#     foo bar bear
#bar    3         
#bear   4   1     
#boat   3   2    2
0
Sandipan Dey On

Probably you want method = 'lv' as an argument to the function:

library(stringdist)
a <- c("foo", "bar", "bear", "boat")
stringdistmatrix(a, method = "lv", useNames = TRUE)

#     foo bar bear
#bar    3         
#bear   4   1     
#boat   3   2    2

You may use adist from base R also to get a distance matrix (with generalized Levenshtein distance) as follows:

matrix(adist(c("foo", "bar", "bear", "boat")), nrow=length(a), dimnames=list(a, a))

#     foo bar bear boat
#foo    0   3    4    3
#bar    3   0    1    2
#bear   4   1    0    2
#boat   3   2    2    0

or a lower triangular dist

as.dist(matrix(adist(c("foo", "bar", "bear", "boat")), 
               nrow=length(a), dimnames=list(a, a)))

#     foo bar bear
#bar    3         
#bear   4   1     
#boat   3   2    2