pmatch {base}R Documentation

Partial String Matching

Description

pmatch seeks matches for the elements of its first argument among those of its second.

Usage

pmatch(x, table, nomatch = NA, duplicates.ok = FALSE)

Arguments

x the values to be matched.
table the values to be matched against.
nomatch the value returned at non-matching or mutliply partially matching positions.
duplicates.ok should elements be in table be used more than once?

Details

The behaviour differs by the value of duplicates.ok. Consider first the case if this is true. First exact matches are considered, and the positions of the first exact matches are recorded. Then unique partial matches are considered, and if found recorded. Finally, all remaining elements of x are regarded as unmatched. In addition, an empty string can match nothing, not even an exact match to an empty string. This is the appropriate behaviour for partial matching of character indices, for example

If duplicates.ok is FALSE, values of table once matched are excluded from the search for subsequent matches. This behaviour is equivalent to the R algorithm for argument matching, except for the consideration of empty strings (which in argument matching are matched after exact and partial matching to any remaining arguments).

charmatch is similar to pmatch with duplicates.ok true, the differences being that it differentiates between no match and an ambiguous partial match, it does match empty strings, and it does not allow multiple exact matches.

Value

A numeric vector of integers (including NA if nomatch = NA) of the same length as x, giving the indices of the elements in table which matched, or nomatch.

Note

Versions of R prior to 1.0.0 had a different behaviour that was seriously incompatible with S (and the current version) when duplicates.ok = TRUE.

Author(s)

Of this version, B. D. Ripley.

See Also

match, charmatch and match.arg, match.fun, match.call, for function argument matching etc.

Examples

pmatch("", "")                             # returns NA
pmatch("m",   c("mean", "median", "mode")) # returns NA
pmatch("med", c("mean", "median", "mode")) # returns 2

pmatch(c("", "ab", "ab"), c("abc", "ab"), dup=F)
pmatch(c("", "ab", "ab"), c("abc", "ab"), dup=T)
## compare
charmatch(c("", "ab", "ab"), c("abc", "ab"))

[Package Contents]