Does R have a comparison operator similar to the Like function, for example:
a<-"Is a Fish"
b<-"Fish"
if(b in a){c<-TRUE}
Michael R Howard
Micron Technology Inc. Boise ID.
Fab C Engineering Software (FCES)
Software Engineer
mhoward at micron.com wrote:> Does R have a comparison operator similar to the Like function, for example: > > a<-"Is a Fish" > b<-"Fish" > > if(b in a){c<-TRUE} >How about ?regexpr: R> a="is a fish" R> b="fish" R> regexpr(b,a) [1] 6 attr(,"match.length") [1] 4 R> regexpr(b,a)>0 [1] TRUE R> b="Fish" R> regexpr(b,a)>0 [1] FALSE Note Regards, Sundar
Have you considered "regexpr"? > a<-"Is a Fish" > b<-"Fish" > regexpr(b, a) [1] 6 attr(,"match.length") [1] 4 hth. spencer graves mhoward at micron.com wrote:> Does R have a comparison operator similar to the Like function, for example: > > a<-"Is a Fish" > b<-"Fish" > > if(b in a){c<-TRUE} > > Michael R Howard > Micron Technology Inc. Boise ID. > Fab C Engineering Software (FCES) > Software Engineer > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
On Thu, May 29, 2003 at 05:11:24PM -0600, mhoward at micron.com wrote:> Does R have a comparison operator similar to the Like function, for example: > > a<-"Is a Fish" > b<-"Fish" > > if(b in a){c<-TRUE}You probably want grep:> a<-"Is a Fish" > b<-"Fish" > if (grep(b,a)) c<-TRUE > c[1] TRUE Hth, Dirk -- Don't drink and derive. Alcohol and analysis don't mix.
OK, regexpr gets me what I needed, thanks to all.. One more thing, say I have a Table like: 0 RAW1 RAW2 RAW3 AVE1 AVE2 AVE3 1 1 2 5 2.3 1.2 4.5 2 0 3 6 1.7 2.2 3.5 3 3 1 6 0.1 3.9 1.6 and I want to create a sub table that only has the RAW columns. Problem is I won't know what the column names will be, only that they either contain "RAW" or "AVE" and they may not be grouped together. Mike -----Original Message----- From: Don MacQueen [mailto:macq at llnl.gov] Sent: Friday, May 30, 2003 8:48 AM To: Dirk Eddelbuettel; mhoward Cc: r-help at stat.math.ethz.ch Subject: Re: [R] Comparison Operator grep() by itself isn't quite right for this job:> a<-"Is a Fish" > b<-"aFish" > if (grep(b,a)) c<-TRUEError in if (grep(b, a)) c <- TRUE : argument is of length zero -Don At 6:24 PM -0500 5/29/03, Dirk Eddelbuettel wrote:>On Thu, May 29, 2003 at 05:11:24PM -0600, mhoward at micron.com wrote: >> Does R have a comparison operator similar to the Like function, for example: >> >> a<-"Is a Fish" >> b<-"Fish" >> >> if(b in a){c<-TRUE} > >You probably want grep: > > > a<-"Is a Fish" >> b<-"Fish" >> if (grep(b,a)) c<-TRUE > > c >[1] TRUE > >Hth, Dirk > >-- >Don't drink and derive. Alcohol and analysis don't mix. > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help-- -------------------------------------- Don MacQueen Environmental Protection Department Lawrence Livermore National Laboratory Livermore, CA, USA
Thanks Rolf and J.R. both of these solutions worked for me and
thanks to all the others for your suggestions, I was able to
accomplish what I needed.
xx <- df[,grep('RAW',names(df))]
xxx <- df[,regexpr('RAW',names(df)) > 0]
Mike
-----Original Message-----
From: mhoward
Sent: Friday, May 30, 2003 9:00 AM
To: 'Don MacQueen'; Dirk Eddelbuettel
Cc: r-help at stat.math.ethz.ch
Subject: RE: [R] Comparison Operator
OK, regexpr gets me what I needed, thanks to all..
One more thing, say I have a Table like:
0 RAW1 RAW2 RAW3 AVE1 AVE2 AVE3
1 1 2 5 2.3 1.2 4.5
2 0 3 6 1.7 2.2 3.5
3 3 1 6 0.1 3.9 1.6
and I want to create a sub table that only has the RAW columns.
Problem is I won't know what the column names will be, only that
they either contain "RAW" or "AVE" and they may not be
grouped
together.
Mike
-----Original Message-----
From: Don MacQueen [mailto:macq at llnl.gov]
Sent: Friday, May 30, 2003 8:48 AM
To: Dirk Eddelbuettel; mhoward
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Comparison Operator
grep() by itself isn't quite right for this job:
> a<-"Is a Fish"
> b<-"aFish"
> if (grep(b,a)) c<-TRUE
Error in if (grep(b, a)) c <- TRUE : argument is of length zero
-Don
At 6:24 PM -0500 5/29/03, Dirk Eddelbuettel wrote:>On Thu, May 29, 2003 at 05:11:24PM -0600, mhoward at micron.com wrote:
>> Does R have a comparison operator similar to the Like function, for
example:
>>
>> a<-"Is a Fish"
>> b<-"Fish"
>>
>> if(b in a){c<-TRUE}
>
>You probably want grep:
>
> > a<-"Is a Fish"
>> b<-"Fish"
>> if (grep(b,a)) c<-TRUE
> > c
>[1] TRUE
>
>Hth, Dirk
>
>--
>Don't drink and derive. Alcohol and analysis don't mix.
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA