Levenshtein distance between two strings
To calculate Levenshtein distance in the R Language, we use the stringdist() function of the stringdist package library. The stringdist() function takes two strings as arguments and returns the Levenshtein distance between them.
Syntax: stringdist( string1, string2, method=”lv” )
Parameter:
- string1 and string2: determine the string whose Levenshtein distance is to be calculated.
Example: Here, we will calculate the Levenshtein distance between two strings.
R
# load library stringdist library (stringdist) # sample strings string1= "Priyank" string2= "w3wiki" # calculate Levenshtein Distance stringdist (string1, string2, method = 'lv' ) |
Output:
How to Calculate Levenshtein Distance in R?
In this article, we will discuss how to calculate Levenshtein Distance in the R Programming Language.
The Levenshtein distance between two strings is the minimum number of character substitutions, insertions, and deletions required to turn one string into the other string. The Levenshtein distance practically is used in approximate string matching, spell-checking, natural language processing, etc.
To calculate the Levenshtein distance in the R Language, we use the stringdist() function of the stringdist package library. The stringdist package is an R Language library that contains approximate String Matching, Fuzzy Text Search, and String Distance functions. The stringdist() function computes pairwise string distances between two or more strings, vectors, or data frame columns.