Reverse grep

Share:

Description

Find which pattern matches x.

Usage

1
2
3
rgrep(pattern, x, ignore.case = FALSE, perl = FALSE, 
  value = FALSE, fixed = FALSE, useBytes = FALSE, 
  invert = FALSE)

Arguments

pattern

a character vector of regular expressions to be matched to x

x

a character string or vector for which a matching regular expression is desired.

ignore.case, perl, value, fixed, useBytes, invert

as for grep

Details

1. np <- length(pattern)

2. g. <- rep(NA, np)

3. for(i in seq(length=np)) g.[i] <- (length(grep(pattern[i], x))>0)

4. return(which(g.))

Value

an integer vector of indices of elements of pattern with a match in x.

Author(s)

Spencer Graves

See Also

grep, pmatch

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
##
## 1.  return index 
##
dd <- data.frame(a = gl(3,4), b = gl(4,1,12)) # balanced 2-way
mm <- model.matrix(~ a + b, dd)

b. <- rgrep(names(dd), colnames(mm)[5])
# check 

all.equal(b., 2)

##
## 2.  return value 
##
bv <- rgrep(names(dd), colnames(mm)[5], value=TRUE)
# check 

all.equal(bv, 'b')

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.