pmatchSafe: Partial matching of strings with error checking

View source: R/pmatchSafe.r

pmatchSafeR 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 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).

error

Logical: If no match is found, return an error?

ignoreCase

Logical: If TRUE (default), ignore the case of values in x and table when checking for matches.

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 error).

...

Arguments to pass to pmatch.

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
)


adamlilith/omnibus documentation built on April 12, 2024, 8:46 p.m.