# RE: [R] Problem with the grep function

From: Marc Mamin <M.Mamin_at_intershop.de>
Date: Fri 09 Jul 2004 - 18:55:19 EST

Hi,

you can use regular expression with grep.

For example:

>exactmatch<-function(s,l){return(grep(paste('^',s,'\$',sep=''),l))}

>t<-c('a','ab','abc','c','ca','ab')

> exactmatch('ab',t)

[1] 2 6

HTH Marc

-----Original Message-----
From: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch]On Behalf Of Petr Pikal Sent: Thursday, July 08, 2004 2:28 PM
To: aubert@inapg.fr; r-help@stat.math.ethz.ch Subject: Re: [R] Problem with the grep function

Hi

You can use %in%

> nom%in%"b"

[1] FALSE TRUE FALSE which gives you a logical vector of exact matches

> (1:3)[nom%in%"b"]
[1] 2

or charmatch

if you expect only one exact match.

But I expect someone can give you better answer.

Cheers
Petr

On 8 Jul 2004 at 9:20, aubert@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@stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal@precheza.cz

R-help@stat.math.ethz.ch mailing list