agrep {base}R Documentation

Approximate String Matching (Fuzzy Matching)


Searches for approximate matches to pattern (the first argument) within each element of the string x (the second argument) using the generalized Levenshtein edit distance (the minimal possibly weighted number of insertions, deletions and substitutions needed to transform one string into another).


agrep(pattern, x, max.distance = 0.1, costs = NULL, = FALSE, value = FALSE, fixed = TRUE,
      useBytes = FALSE)

agrepl(pattern, x, max.distance = 0.1, costs = NULL, = FALSE, fixed = TRUE, useBytes = FALSE)



a non-empty character string to be matched. For fixed = FALSE this should contain an extended regular expression. Coerced by as.character to a string if possible.


character vector where matches are sought. Coerced by as.character to a character vector if possible.


maximum distance allowed for a match. Expressed either as integer, or as a fraction of the pattern length times the maximal transformation cost (will be replaced by the smallest integer not less than the corresponding fraction), or a list with possible components


maximum number/fraction of match cost (generalized Levenshtein distance)


maximal number/fraction of all transformations (insertions, deletions and substitutions)


maximum number/fraction of insertions


maximum number/fraction of deletions


maximum number/fraction of substitutions

If cost is not given, all defaults to 10%, and the other transformation number bounds default to all. The component names can be abbreviated.


a numeric vector or list with names partially matching ‘⁠insertions⁠’, ‘⁠deletions⁠’ and ‘⁠substitutions⁠’ giving the respective costs for computing the generalized Levenshtein distance, or NULL (default) indicating using unit cost for all three possible transformations. Coerced to integer via as.integer if possible.

if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.


if FALSE, a vector containing the (integer) indices of the matches determined is returned and if TRUE, a vector containing the matching elements themselves is returned.


logical. If TRUE (default), the pattern is matched literally (as is). Otherwise, it is matched as a regular expression.


logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See ‘Details’.


The Levenshtein edit distance is used as measure of approximateness: it is the (possibly cost-weighted) total number of insertions, deletions and substitutions required to transform one string into another.

This uses the tre code by Ville Laurikari (, which supports MBCS character matching.

The main effect of useBytes = TRUE is to avoid errors/warnings about invalid inputs and spurious matches in multibyte locales. It inhibits the conversion of inputs with marked encodings, and is forced if any input is found which is marked as "bytes" (see Encoding).


agrep returns a vector giving the indices of the elements that yielded a match, or, if value is TRUE, the matched elements (after coercion, preserving names but no other attributes).

agrepl returns a logical vector.


Since someone who read the description carelessly even filed a bug report on it, do note that this matches substrings of each element of x (just as grep does) and not whole elements. See also adist in package utils, which optionally returns the offsets of the matched substrings.


Original version in R < 2.10.0 by David Meyer. Current version by Brian Ripley and Kurt Hornik.

See Also

grep, adist. A different interface to approximate string matching is provided by aregexec().


agrep("lasy", "1 lazy 2")
agrep("lasy", c(" 1 lazy 2", "1 lasy 2"), max.distance = list(sub = 0))
agrep("laysy", c("1 lazy", "1", "1 LAZY"), max.distance = 2)
agrep("laysy", c("1 lazy", "1", "1 LAZY"), max.distance = 2, value = TRUE)
agrep("laysy", c("1 lazy", "1", "1 LAZY"), max.distance = 2, = TRUE)

[Package base version 4.4.1 Index]