[R] regular expressions with grep() and negative indexing

Stephen Tucker brown_emu at yahoo.com
Wed Apr 25 12:15:33 CEST 2007


Dear R-helpers,

Does anyone know how to use regular expressions to return vector elements
that don't contain a word? For instance, if I have a vector
  x <- c("seal.0","seal.1-exclude")
I'd like to get back the elements which do not contain the word "exclude",
using something like (I know this doesn't work) but:
  grep("[^(exclude)]",x)

I can use 
  x[-grep("exclude",x)]
for this case but then if I use this expression in a recursive function, it
will not work for instances in which the vector contains no elements with
that word. For instance, if I have
  x2 <- c("dolphin.0","dolphin.1")
then
  x2[-grep("exclude",x2)]
will give me 'character(0)'

I know I can accomplish this in several steps, for instance:
  myfunc <- function(x) {
    iexclude <- grep("exclude",x)
    if(length(iexclude) > 0) x2 <- x[-iexclude] else x2 <- x
    # do stuff with x2 <...?
  }

But this is embedded in a much larger function and I am trying to minimize
intermediate variable assignment (perhaps a futile effort). But if anyone
knows of an easy solution, I'd appreciate a tip.

Thanks very much!

Stephen



More information about the R-help mailing list