Skip to content
Prev 22846 / 63424 Next

invert argument in grep

On 11/10/2006 12:52 PM, Romain Francois wrote:
That's good for intersecting two searches, but not for other boolean 
combinations.

My main point was that inversion isn't the only boolean operation you 
may want, but R has perfectly good powerful boolean operators, so 
installing a limited subset of boolean algebra into grep() is probably 
the wrong approach.
Again, it covers & and !, but it misses other boolean operators.
If you're willing to write these, then it's easy to write igrep without 
an invert arg to grep:

igrep <- function(pat, x, ...)
    setdiff(1:length(x), grep(pat, x, value = FALSE, ...))

ivgrep would also be easy, except for the weird semantics of value=TRUE 
pointed out by Brian:  but it could still be written with a little bit 
of care.

Duncan Murdoch