#' Search strings for multiple patterns
#'
#' @param patterns character string containing a regular expression
#' (or character string)
#' @param x character vector
#' @param ignore.case if \code{FALSE}, the pattern matching is case
#' sensitive and if \code{TRUE}, case is ignored
#' during matching
#' @param perl logical. Should perl-compatible regexps be used?
#' @param value if \code{FALSE}, a vector containing the (integer)
#' indices of the matches determined by grep is
#' returned, and if \code{TRUE}, a vector containing
#' the matching elements themselves is returned
#' @param fixed logical. If \code{TRUE}, pattern is a string to
#' be matched as is. Overrides all conflicting arguments
#' @param useBytes logical. If \code{TRUE} the matching is done byte-by-byte
#' rather than character-by-character.
#' @param invert logical. If \code{TRUE} return indices or values for elements
#' that do not match.
#' @param list if \code{TRUE} list all matches
#' @param action function for aggregating matches, e.g. \code{all}, \code{any},
#' \code{sum} (count maches), \code{mean} (proportion of matching)
#'
#' @seealso \code{\link{grep}}
#' @export
mgrep <- function(patterns, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
fixed = FALSE, useBytes = FALSE, invert = FALSE, list = FALSE) {
out <- lapply(patterns, function(p) grep(p, x, ignore.case = ignore.case,
perl = perl, value = value, fixed = fixed,
useBytes = useBytes, invert = invert))
if (list) {
names(out) <- patterns
out
} else {
unlist(out)
}
}
#' @rdname mgrep
#' @export
mgrepl <- function(patterns, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE,
useBytes = FALSE, action = "list") {
out <- lapply(patterns, function(p) grepl(p, x, ignore.case = ignore.case,
perl = perl, fixed = fixed,
useBytes = useBytes))
names(out) <- patterns
if (action == "list") {
out
} else {
apply(as.data.frame(out), 1, action)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.