[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  pmatch: 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_integer_, duplicates.ok = FALSE)

##  Arguments

x

the values to be matched: converted to a character vector by ` [ as.character
](/link/as.character?package=base&version=3.6.2) ` . [ Long vectors
](/link/Long%20vectors?package=base&version=3.6.2) are supported.

table

the values to be matched against: converted to a character vector. [ Long
vectors ](/link/Long%20vectors?package=base&version=3.6.2) 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?

##  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 the consideration of
empty strings (which in argument matching are matched after exact and partial
matching to any remaining arguments).

` [ charmatch ](/link/charmatch?package=base&version=3.6.2) ` 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.

` NA ` values are treated as if they were the string constant ` "NA" ` .

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

Chambers, J. M. (1998) _Programming with Data. A Guide to the S Language_ .
Springer.

##  See Also

` [ match ](/link/match?package=base&version=3.6.2) ` , ` [ charmatch
](/link/charmatch?package=base&version=3.6.2) ` and ` [ match.arg
](/link/match.arg?package=base&version=3.6.2) ` , ` [ match.fun
](/link/match.fun?package=base&version=3.6.2) ` , ` [ match.call
](/link/match.call?package=base&version=3.6.2) ` , for function argument
matching etc., ` [ startsWith ](/link/startsWith?package=base&version=3.6.2) `
for particular checking of initial matches; ` [ grep
](/link/grep?package=base&version=3.6.2) ` etc for more general (regexp)
matching of strings.

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fpmatch&utm_source=r-docs&utm_medium=docs&utm_term=pmatch&utm_content=run_example_in_workspace)

    
    
    # 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")