pmatch
Partial Matching of Character Items in a Vector

Description

Returns, for each element of x, the position in table whose initial substring matches that element.

Usage

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

Arguments

x a character vector of items to match sequentially in table, using the partial match algorithm.
table a character vector giving the possible values in x.
nomatch the value to return when an item in x does not uniquely match any item in table.
duplicates.ok a logical value. If set to FALSE (the default), duplicate values in x correspond to nomatch. You can use this technique to simulate function call name matching.

If set to TRUE, matches duplicate values. You can use this technique to simulate character subset calculations.

Details

Matching is done by the same algorithm used to match named arguments in function calls. See Section 11.3.5 of Becker, Chambers and Wilks (1988). Note that pmatch does not match empty strings, in contrast to the argument-matching algorithm, which matches unnamed arguments positionally after handling named arguments.
The charmatch function is similar to pmatch. See the documentation for charmatch for a comparison of their differences.
Value
a numeric vector like x giving, for each element of x, the position in table of the first table[i] whose initial substring uniquely matches that element.
References
Becker, R.A., Chambers, J.M., and Wilks, A.R. (1988). The New S Language Wadsworth and Brooks/Cole, Pacific Grove, CA.
See Also
charmatch, casefold, match.
Examples
pmatch("Ala", Sdatasets::state.name)  # is NA (could match Alaska or Alabama)
pmatch("Alab", Sdatasets::state.name)  # is 1, matching Alabama

# Given names, possibly truncated versions of state names # produce the full state names and skip the ones that don't match # by setting nomatch to zero. names <- c('Flo', 'Zzz', 'Mon') Sdatasets::state.name[pmatch(names, Sdatasets::state.name, nomatch=0)]

Package base version 6.0.0-69
Package Index