StrDist {DescTools}  R Documentation 
StrDist
computes distances between strings following to Levenshtein or Hamming method.
StrDist(x, y, method = "levenshtein", mismatch = 1, gap = 1, ignore.case = FALSE)
x 
character vector, first string. 
y 
character vector, second string. 
method 
character, name of the distance method. This must be

mismatch 
numeric, distance value for a mismatch between symbols. 
gap 
numeric, distance value for inserting a gap. 
ignore.case 
if 
The function computes the Hamming and the Levenshtein (edit) distance of two given strings (sequences). The Hamming distance between two vectors is the number mismatches between corresponding entries.
In case of the Hamming distance the two strings must have the same length.
In case of the Levenshtein (edit) distance a scoring and a traceback matrix are computed
and are saved as attributes "ScoringMatrix"
and "TraceBackMatrix"
.
The numbers in the traceback matrix reflect insertion of a gap in string y
(1),
match/missmatch (2), and insertion of a gap in string x
(3).
The edit distance is useful, but normalizing the distance to fall within the interval [0,1] is
preferred because it is somewhat diffcult to judge whether an LD of for example 4 suggests a high or low degree of similarity.
The method "normlevenshtein"
for normalizing the LD is sensitive to this scenario.
In this implementation, the Levenshtein distance is transformed to fall in this interval as
follows:
lnd = 1  \frac{ld}{max(length(x), length(y))}
where ld
is the edit distance and max(length(x), length(y))
denotes that we divide by the length of the larger of the two character strings. This normalization, referred to as the Levenshtein normalized distance (lnd), yields a statistic where 1 indicates perfect agreement between the two strings, and a 0 denotes imperfect agreement. The closer a value is to 1, the more certain we can be that the character strings are the same; the closer to 0, the less certain.
StrDist
returns an object of class "dist"
; cf. dist
.
For distances between strings and for string alignments see also Bioconductor package Biostrings
Matthias Kohl <Matthias.Kohl@stamats.de>
R. Merkl and S. Waack (2009) Bioinformatik Interaktiv. Wiley.
Harold C. Doran (2010) MiscPsycho. An R Package for Miscellaneous Psychometric Analyses
x < "GACGGATTATG"
y < "GATCGGAATAG"
## Levenshtein distance
d < StrDist(x, y)
d
attr(d, "ScoringMatrix")
attr(d, "TraceBackMatrix")
## Hamming distance
StrDist(x, y, method="hamming")