StrDist {DescTools}R Documentation

Compute Distances Between Strings


StrDist computes distances between strings following to Levenshtein or Hamming method.


StrDist(x, y, method = "levenshtein", mismatch = 1, gap = 1, = FALSE)



character vector, first string.


character vector, second string.


character, name of the distance method. This must be "levenshtein", "normlevenshtein" or "hamming". Default is "levenshtein", the classical Levenshtein distance.


numeric, distance value for a mismatch between symbols.


numeric, distance value for inserting a gap.

if FALSE (default), the distance measure will be case sensitive and if TRUE, case is ignored.


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 trace-back matrix are computed and are saved as attributes "ScoringMatrix" and "TraceBackMatrix". The numbers in the trace-back 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 <>


R. Merkl and S. Waack (2009) Bioinformatik Interaktiv. Wiley.

Harold C. Doran (2010) MiscPsycho. An R Package for Miscellaneous Psychometric Analyses

See Also

adist, dist


## Levenshtein distance
d <- StrDist(x, y)
attr(d, "ScoringMatrix")
attr(d, "TraceBackMatrix")

## Hamming distance
StrDist(x, y, method="hamming")

[Package DescTools version 0.99.51 Index]