Skip to content
Prev 22842 / 63424 Next

invert argument in grep

On 11/9/2006 5:14 AM, Romain Francois wrote:
I think a more generally useful change would be to be able to return a 
logical vector with TRUE for a match and FALSE for a non-match, so a 
simple !grep(...) does the inversion.  (This is motivated by the recent 
R-help discussion of the fact that x[-selection] doesn't always invert 
the selection when it's a vector of indices.)

A way to do that without expanding the argument list would be to allow

value="logical"

as well as value=TRUE and value=FALSE.

This would make boolean operations easy, e.g.

colors()[grep("dark", colors(), value="logical")
       & !grep("blue", colors(), value="logical")]

to select the colors that contain "dark" but not "blue". (In this case 
the RE to select that subset is rather simple because "dark" always 
precedes "blue", but if that wasn't true, it would be a lot messier.)

Duncan Murdoch