Levenshtein distance between two string vectors
To calculate the Levenshtein distance between two vectors in the R Language, we use the stringdist() function of the stringdist package library. The stringdist() function takes two string vectors as arguments and returns a vector that contains the Levenshtein distance between each string pair in them.
Syntax: stringdist( string_vec1, string_vec2, method=”lv” )
Parameter:
- string_vec1 and string_vec2: determine the string vectors whose Levenshtein distance is to be calculated.
Example: Here, we will calculate the Levenshtein distance between two string vectors.
R
# load library stringdist library (stringdist) # sample strings string_vec1<- c ( "Priyank" , "Abhiraj" , "Sudhanshu" ) string_vec2<- c ( "w3wiki" , "Devraj" , "Pawan" ) # calculate Levenshtein Distance stringdist (string_vec1, string_vec2, 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.