The stringdist package provides efficient algorithms for computing string distances between character vectors. It supports multiple distance methods including optimal string alignment, Levenshtein, Damerau-Levenshtein, Hamming, longest common subsequence, q-gram, cosine, Jaccard, Jaro-Winkler, and Soundex.
Core Functions
stringdist()
This function calculates distances pairwise between elements of two character vectors:
stringdist(a, b, method = "osa", useBytes = FALSE,
weight = c(d = 1, i = 1, s = 1, t = 1),
maxDist = Inf, q = 1, p = 0, nthread = 1)
stringdistmatrix()
This function returns a distance matrix with elements from the first vector as rows and the second vector as columns:
stringdistmatrix(a, b, method = "osa", useBytes = FALSE,
weight = c(d = 1, i = 1, s = 1, t = 1),
maxDist = Inf, q = 1, useNames = "none",
ncores = 1, nthread = 1)
Parameter Reference
| Praameter | Description |
|---|---|
a, b |
Character vectors to compare |
method |
Distance algorithm: "osa", "lv", "dl", "hamming", "lcs", "qgram", "cosine", "jaccard", "jw", "soundex" |
useBytes |
Perform byte-level comparison instead of character-level |
weight |
Vector of weights for deletion, insertion, substitution, and transposition (default c(d=1,i=1,s=1,t=1)) |
maxDist |
Pruning threshold - distances exceeding this value are not computed |
q |
Q-gram size for qgram, jaccard, and cosine methods |
p |
Prefix penalty for Jaro-Winkler (range 0-0.25) |
nthread |
Number of parallel threads |
useNames |
Label rows/columns with input string values |
Usage Examples
Basic Disatnce Matrix
stringdistmatrix(c("cat", "dog", "rat"), c("bat", "bot"))
[,1] [,2]
[1,] 1 2
[2,] 3 1
[3,] 1 2
Case Sensitivity
stringdist("HELLO", "hello")
[1] 5
stringdist(tolower("HELLO"), "hello")
[1] 0
Vector Recycling
stringdist(c('x','y','z'), c('x','z'))
Warning message: longer object length is not a multiple of shorter object length
[1] 0 1 1
Custom Operation Weights
stringdist('cd', 'dc', weight = c(1, 1, 1, 0.5))
[1] 0.5
Asymmetric Distance Metrics
stringdist('vs', 'abc')
[1] 3
stringdist('abc', 'vs')
[1] 3
stringdist('vs', 'abc', weight = c(0.5, 1, 1, 1))
[1] 2
stringdist('abc', 'vs', weight = c(0.5, 1, 1, 1))
[1] 2.5
Q-gram Distance
The q-gram method counts occurrences of consecutive character sequences:
stringdist('xyz', 'zyx', method = 'qgram', q = 1)
[1] 0
stringdist('xyz', 'zyx', method = 'qgram', q = 2)
[1] 4
Jaro-Winkler Distance
stringdist('STACY', 'STAICY', method = 'jw')
[1] 0.08333333
1 - stringdist('STACY', 'STAICY', method = 'jw')
[1] 0.9166667
stringdist('STACY', 'STAICY', method = 'jw', p = 0.1)
[1] 0.06666667
Soundex Encoding
stringdist('Newton', 'Newtonian', method = 'soundex')
[1] 1
stringdist('Newton', 'Newtone', method = 'soundex')
[1] 0
Converting Distance to Similarity
To express results as similarity rather than distance, apply the transformation: 1 - distance. For Levenshtein distance specifically:
similarity <- 1 - (levenshteinDist(str1, str2) / max(nchar(str1), nchar(str2)))
The RecordLinkage package provides levenshteinSim() for direct similarity computation:
library(RecordLinkage)
levenshteinSim("testing", "testink")
[1] 0.875
levenshteinSim("testing", "testing")
[1] 1