Learn R Programming

Kmisc (version 0.5.0)

ngrep: Pattern matching and Replacement

Description

These functions provide simple extensions to base regular expressions in R, primarily intended to assist with extraction of elements based on the result of a regular expression evaluation.

Usage

ngrep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE)
fgrep(pattern, x, ignore.case = FALSE, value = FALSE, useBytes = FALSE, invert = FALSE)
re_exists(x, pattern, perl = TRUE, fixed = FALSE, ...)
re.exists(pattern, x, perl = TRUE, fixed = FALSE, ...)
re_extract(x, pattern, perl = TRUE, fixed = FALSE, ...)
extract.re(x, pattern, perl = TRUE, fixed = FALSE, ...)
re_without(x, pattern, perl = TRUE, fixed = FALSE, ...)
without.re(x, pattern, perl = TRUE, fixed = FALSE, ...)
re_extract_rows(x, pattern, match_var = rownames(x), perl = TRUE, fixed = FALSE, ...)
extract_rows.re(x, pattern, match_var = rownames(x), perl = TRUE, fixed = FALSE, ...)
re_without_rows(x, pattern, match_var = rownames(x), perl = TRUE, fixed = FALSE, ...)
without_rows.re(x, pattern, match_var = rownames(x), perl = TRUE, fixed = FALSE, ...)

Arguments

x
An R object (in the case of re_ methods), or a character vector (in the case of ngrep, fgrep)
pattern
character string containing a character string to be matched in the given character vector; coerced by as.character if possible.
ignore.case
boolean; if TRUE we perform case-insensitive matching.
perl
boolean; if TRUE, we use perl-compatible regular expressions.
value
boolean; if TRUE we return the actual matches; if FALSE we return the indices corresponding to the matches.
fixed
boolean; if TRUE the pattern is matched as-is. Overrides all conflicting arguments.
useBytes
boolean; if TRUE we perform matching byte-by-byte rather than character by character.
match_var
A variable to match on, as used in the re_extract_rows function.
invert
Invert the results of the regular expression match?
...
Optional arguments passed to grep or grepl.

Details

The order is reversed for the re_ set of functions; i.e., an R object is expected first, rather than a regular expression pattern.

See Also

grep

grep, regex

Examples

Run this code
ngrep( "abc", c("abc", "babcd", "abcdef", "apple"), value=TRUE )
if( re_exists(c("apple", "banana"), "^ap") ) print("yay!")
dat <- data.frame( x=letters, y=LETTERS )
rownames(dat) <- 1:26
## get all rows in dat with a 1, 2, 3 or 4 in the name
re_extract_rows( dat, "[1-4]" )
dat <- data.frame( x=letters, y=LETTERS )
rownames(dat) <- 1:26
## get all rows in dat with a 1, 2, 3 or 4 in the name
re_without_rows( dat, "[0-4]" )

Run the code above in your browser using DataLab