R 在编辑距离中维护标题

R 在编辑距离中维护标题,r,edit-distance,stringdist,R,Edit Distance,Stringdist,我正在使用stringdist运行编辑距离。输出用编号列表代替输入,而不是要比较的实际字符串。这就是我目前拥有的: library(stringdist) a <- c("foo", "bar", "bear", "boat", method = "lv") stringdistmatrix(a) 1 2 3 2 3 3 4 1 4 3 2 2 您可以指定useNames参数: stringdistmatrix(a, useNames = TRUE) # foo

我正在使用
stringdist
运行编辑距离。输出用编号列表代替输入,而不是要比较的实际字符串。这就是我目前拥有的:

library(stringdist)

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

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

您可以指定
useNames
参数:

stringdistmatrix(a, useNames = TRUE)

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

您可能希望
method='lv'
作为函数的参数:

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
或下三角区

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
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
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