Learn R Programming

miscset (version 0.3.1)

mgrepl: Multiple Pattern Matching and Replacement

Description

mgrepl searches for any or all patterns and returns logical values. Combination of the results is done via the logic functions any or all. Multicore feature is made available by parallel:::mclapply. msub and mgsub replace multiple patterns in a vector by the order of

Usage

mgrepl(patterns, text, log.fun = any, cores = 1, ...)

msub(patterns, replacement, text, ...)

mgsub(patterns, replacement, text, ...)

Arguments

patterns
A character vector containing a regular expression (regex) to be searched in text.
text
Character vector where the search and replace is performed.
replacement
Character vector with the replacement pattern.
log.fun
For mgrepl. Logical function (any or all) to evaluate occurence of each pattern in patterns in each value of text. Can also be custom. See examples.
cores
Numeric value for how many cores to use for computation using mclapply.
...
Further arguments passed to functions grepl(), sub() and gsub().

Value

  • msub and mgsub: Same as text but with all patterns replaced. mgrepl: Logical vector of sam length as text where TRUE means either any or all patterns in patternlist are matched in text depending on log.fun.

See Also

grep, mclapply

Examples

Run this code
#

# Compare different "log.fun" parameters:
mgrepl(c("a","b"), c("ab","ac","bc"), any)
mgrepl(c("a","b"), c("ab","ac","bc"), all)
mgrepl(letters[1:3], c("ax","xab","xbc"), function (x) sum(x)>1)

# Replace several patterns (gplobally):
msub(letters[1:3], "<replaced>", letters[1:5])
mgsub(letters[1:2], "<replaced>", c("ab","ba","acb",NA))

#

Run the code above in your browser using DataLab