mgrep: multi-grep methods

Description Usage Arguments Details Value Author(s) Examples

Description

a collection of multi-grep methods which make use of multiple patterns, as opposed to grep's single pattern.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
mgrep(patterns, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
  fixed = FALSE, useBytes = FALSE, invert = FALSE, nomatch = NA)

mgrepl(patterns, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE)

grepf(patterns, x, ignore.case = FALSE, perl = FALSE, value = FALSE,
  fixed = FALSE, useBytes = FALSE, invert = FALSE)

grepfl(patterns, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE,
  useBytes = FALSE, invert = FALSE)

Arguments

patterns

a character vector of at least one pattern.

x

a character vector where matches are sought, or an object which can be coerced by as.character to a character vector.

ignore.case

if FALSE, the pattern matching is case sensitive and if TRUE, case is ignored during matching.

perl

logical. Should Perl-compatible regexps be used?

value

if FALSE, a vector containing the (integer) indices of the matches determined by grep is returned, and if TRUE, a vector containing the matching elements themselves is returned.

fixed

logical. If TRUE, pattern is a string to be matched as is. Overrides all conflicting arguments.

useBytes

logical. If TRUE the matching is done byte-by-byte rather than character-by-character. See ‘Details’.

invert

logical. If TRUE return indices or values for elements that do not match.

nomatch

if a pattern isn't found, what should be returned for that element in the result list? default=NA which just reports an NA. NULL is allowed, in which case a numeric(0) or character(0) (if value=TRUE) is put in the corresponding element of the list.

Details

mgrep: mgrep takes a vector of patterns, and searches for each within x, returning either the matching indices into x, or values (if value=TRUE), for each pattern in a list of length(patterns). Just like mget as the multi-version of get, there is a nomatch parameter controlling what to do if no match for a particular pattern is found.

mgrepl: grep for multiple patterns, and return logical if each pattern is present in any value of x

grepf: grep -f for R. ie return the elements of x which match any of the patterns.

grepfl: combo of grepf + grepl ie which elements of x match >=1 of the patterns, as a logical vector

Value

mgrep: a list with one element per pattern in the patterns vector. Each element of this list is the value returned by grep for each pattern, OR nomatch if nothing is found. NB this is different from the default behaviour of grep, which returns numeric(0) when no match is found.

mgrepl: a logical vector, length = length(patterns), indicating whether each pattern was found in x

grepf: if value=FALSE, a numeric vector of indices in 1:length(x) which match any of the patterns, or, if value=TRUE, return a character vector of the elements of x which match any of the patterns.

grepfl: a logical vector, of length=length(x), indicating whether each of the elements in x match any of the patterns. if invert=TRUE, then the logical vector indicates which elements did not match any of the patterns.

Author(s)

Mark Cowley, 23 Sept 2005

Mark Cowley, 2011-02-18

Mark Cowley, 2011-02-18

Mark Cowley, 2011-02-18

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# use the state.name example data
mgrep(c("New","^V", "Iran"), state.name)
mgrep(c("New","^V", "Iran"), state.name, value=TRUE)

# nomatch
mgrep(c("New","^V", "Iran"), state.name, nomatch=NULL)
mgrep(c("New","^V", "Iran"), state.name, value=TRUE, nomatch=NULL)
# use the state.name example data
mgrepl(c("New","^V", "Iran"), state.name)
#  New    ^V  Iran 
# TRUE  TRUE FALSE 

# use the state.name example data
grepf(c("New","^V", "Iran"), state.name)
# [1] 29 30 31 32 45 46
grepf(c("New","^V", "Iran"), state.name, value=TRUE)
grepf(c("New","^V", "Iran"), state.name, invert=TRUE)
# use the state.name example data
grepfl(c("New","^V", "Iran"), state.name)
grepfl(c("New","^V", "Iran"), state.name, invert=TRUE)

drmjc/mjcbase documentation built on May 15, 2019, 2:27 p.m.