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.
pmatchSafe(
x,
table,
useFirst = FALSE,
error = TRUE,
ignoreCase = TRUE,
nmax = length(x),
...
)
One or more of the values in table
.
Character: String to match.
Character vector: Values to which to match.
Logical: If TRUE
, and there is more than one match for a given x
, then the first value in table
that matches x
will be returned (without an error or warning).
Logical: If no match is found, return an error?
Logical: If TRUE
(default), ignore the case of values in x
and table
when checking for matches.
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 error
).
Arguments to pass to pmatch
.
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
)
Run the code above in your browser using DataLab