mgrepl: Multiple Pattern Matching and Replacement

Description Usage Arguments Value Author(s) See Also Examples

Description

mgrepl allows multiple patterns search in character vectors, offering multicore support to parallelize search over all patterns using mclapply.

Usage

1
2
mgrepl(patterns, text, log.fun = all, na.replace = FALSE,
  use.which = FALSE, cores = 1, ...)

Arguments

patterns

A vector or list containing regular expressions (regex) to be searched in text. Coerced to character.

text

Character vector on which the search is performed.

log.fun

A function to apply on the result of matching each pattern on each element of text. Determines the output. See section Value.

na.replace

A single value to replace each NA with in the result.

use.which

A logical value. TRUE to convert result with which. Only if output is.atomic, otherwise ignored. Deprecated.

cores

Numeric value for how many cores to use for computation using mclapply.

...

Further arguments passed to functions grepl.

Value

Depending on the function defined with log.fun, the return value is either

Author(s)

Sven E. Templer

See Also

grepl, mclapply

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#

# strings
s <- c("ab","ac","bc", NA)

# match all patterns (default)
mgrepl(c("a", "b"), s)

# match any of the patterns
mgrepl(c("a", "b"), s, any)
grepl("a|b", s)

# return logical matrix, one column for each pattern
mgrepl(c("a", "b"), s, identity)

# return count of matches
mgrepl(c("a", "b"), s, sum)

#

Example output

[1]  TRUE FALSE FALSE FALSE
[1]  TRUE  TRUE  TRUE FALSE
[1]  TRUE  TRUE  TRUE FALSE
     [,1]  [,2]  [,3]  [,4]
[1,] TRUE  TRUE FALSE FALSE
[2,] TRUE FALSE  TRUE FALSE
[1] 2 1 1 0

miscset documentation built on May 2, 2019, 4:01 a.m.