ID EN
Miscellaneous

pmatch

R Base 3.6.2

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

Syntax

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

Arguments

Parameter Description
x the values to be matched: converted to a character vector by as.character. Long vectors are supported.
table the values to be matched against: converted to a character vector. Long vectors are not supported.
nomatch the value to be returned at non-matching or multiply partially matching positions. Note that it is coerced to integer.
duplicates.ok should elements be in table be used more than once?

Return Value

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

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. (A partial match occurs if the whole of the element of x matches the beginning of the element of table.) 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 th

Examples

Example
R
# NOT RUN {
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 = FALSE)
pmatch(c("", "ab", "ab"), c("abc", "ab"), dup = TRUE)
## compare
charmatch(c("", "ab", "ab"), c("abc", "ab"))
# }

See Also

match charmatch and match.arg match.fun match.call for function argument matching etc. startsWith for particular checking of initial matches; grep etc for more general (regexp) matching of strings.