pmatchSafe {omnibus} | R Documentation |
Partial matching of strings with error checking
Description
This function is the same as pmatch
, but it can throw an error instead of NA
if not match is found, and can be forced to throw the error if more than the desired number of matches is found.
Usage
pmatchSafe(
x,
table,
useFirst = FALSE,
error = TRUE,
ignoreCase = TRUE,
nmax = length(x),
...
)
Arguments
x |
Character: String to match. |
table |
Character vector: Values to which to match. |
useFirst |
Logical: If |
error |
Logical: If no match is found, return an error? |
ignoreCase |
Logical: If |
nmax |
Positive numeric integer: Maximum allowable number of matches. If more than this number of matches is found, an error will be thrown (regardless of the value of |
... |
Arguments to pass to |
Value
One or more of the values in table
.
Examples
pmatchSafe('ap', c('apples', 'oranges', 'bananas'))
pmatchSafe('AP', c('apples', 'oranges', 'bananas'))
pmatchSafe('AP', c('apples', 'oranges', 'bananas'),
ignoreCase = FALSE, error = FALSE)
pmatchSafe(c('ba', 'ap'), c('apples', 'oranges', 'bananas'))
# No match:
tryCatch(
pmatchSafe('kumquats', c('apples', 'oranges', 'bananas')),
error = function(cond) FALSE
)
pmatchSafe('kumquats', c('apples', 'oranges', 'bananas'), error = FALSE)
pmatchSafe(c('ap', 'corn'), c('apples', 'oranges', 'bananas'), error = FALSE)
# Too many matches:
tryCatch(
pmatchSafe(c('ap', 'ba'), c('apples', 'oranges', 'bananas'), nmax = 1),
error=function(cond) FALSE
)