rstring-matchingnapartialno-match

Properties of pmatch function


I don't understand the behavior of the built-in function pmatch (partial string matching).

The description provides the following example:

pmatch("m",   c("mean", "median", "mode")) # returns NA instead of 1,2,3

but using:

pmatch("m", "mean") # returns 1, as I would have expected. 

Could anybody explain to me this behavior?


Solution

  • As per the documentation:

    nomatch: the value to be returned at non-matching or multiply partially matching positions. Note that it is coerced to integer.

    The nomatch defaults to NA (i.e., if there are multiple partial matches then NA will be returned).

    pmatch("me",   c("mean", "median", "mode")) 
    [1] NA  # returns NA instead of 1,2 since multiple partial matches
    
    pmatch("mo",   c("mean", "median", "mode")) 
    [1] 3   # since single partial match