[R] Problem with the grep function

Uwe Ligges ligges at statistik.uni-dortmund.de
Thu Jul 8 09:54:27 CEST 2004


Wolski wrote:

> Hi!
> 
> For exact matches you can use
> == or is.element.
> To get the indices use which.
> 
> e.g. ==
> 
> 
>>x<-c("a","b","ab")
>>x=="a"
> 
> [1]  TRUE FALSE FALSE
> 
>>which((x=="a")==T)

Note, the "==T" part is superflously (same below).

In grep()'s regular expression, you can also use:
    grep("^b$", nom)
or similar stuff.

Uwe Ligges


> [1] 1
> 
> or
> 
> e.g. is.element
> 
> 
>>is.element(x,"a")
> 
> [1]  TRUE FALSE FALSE
> 
>>which(is.element(x,"a")==TRUE)
> 
> [1] 1
> 
> 
> Sincerely
> Eryk
> 
> 
> *********** REPLY SEPARATOR  ***********
> 
> On 7/8/2004 at 9:20 AM aubert at inapg.fr wrote:
> 
> 
>>>>Let me present to you my problem :
>>>>
>>>>I have a character vector x and I would like to  obtain the indices of
>>>>the 
>>>>elements of
>>>>this vector that yielded exactly a match.
>>>>
>>>>For example,  x=nom, pattern="b", I would to obtain 2 because "b" is on
>>>>the 
>>>>second position.
>>>>
>>>>First program :
>>>>nom <- c("a","b","ab")
>>>>grep("b",nom)
>>>>2 3
>>>>
>>>>Then I try the option extended =FALSE (instead of TRUE by default) and I 
>>>>obtain '2 3' a second time.
>>>>
>>>>Please can you help me : How can I obtain only 2 in using the grep
>>>>function 
>>>>(without using the match function).
>>>>
>>>>Thanks you
>>>>
>>>>Julie AUBERT
>>>>
>>>>______________________________________________
>>>>R-help at stat.math.ethz.ch mailing list
>>>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 
> 
> 
> Dipl. bio-chem. Eryk Witold Wolski    @    MPI-Moleculare Genetic   
> Ihnestrasse 63-73 14195 Berlin       'v'    
> tel: 0049-30-83875219               /   \    
> mail: wolski at molgen.mpg.de        ---W-W----    http://www.molgen.mpg.de/~wolski
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list