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

Tony Plate tplate at acm.org
Wed Apr 25 17:39:23 CEST 2007


I use regexpr() instead of grep() in cases like this, e.g.:

x2[regexpr("exclude",x2)==-1]

(regexpr returns a vector of the same length as character vector given 
it, so there's no problem with it returning a zero length vector)

-- Tony Plate

Peter Dalgaard wrote:
> Stephen Tucker wrote:
>> 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.
>>   
> It has come up a couple of times before, and yes, it is a bit of a pain.
> 
> Probably the quickest way out is
> 
> negIndex <- function(i) 
> 
>    if(length(i))
> 
>        -i 
> 
>    else 
> 
>        TRUE
>



More information about the R-help mailing list