[R] What is wrong with this code?

Peter Ehlers ehlers at ucalgary.ca
Thu Jul 1 00:18:31 CEST 2010


On 2010-06-30 15:56, Cable, Samuel B Civ USAF AFMC AFRL/RVBXI wrote:
>> Okay, here is a reproducible example of a possible solution (actually
>> it
>> probably does not do what you want but it's a start for discussion.
>> Note it is actual code and data which is what is needed.
>> =======================================================
>> str1<- "abc"
>> str2<- "abcd"
>> str3<- "efgh"
>>
>> grep(str1, str2)
>> grep(str1,str3)
>> =======================================================
>
> Thanks for everybody's help on this and thanks for your patience with my
> original mistake.
>
> FWIW, grep doesn't quite do it because if I put it into an "if" test
>
> if (grep(str1,str3) somethingsomething ) { ... }
>
> and str3 does not contain str1, then R gets upset, won't even evaluate
> the "if", and returns an error.
>
> I'll take a look at grepl.  Turns out I don't have it because I am
> currently running rev. 2.8.1-1.  Oh well.
>
> Thanks again.

Well, you certainly should upgrade. But in the meantime,
try wrapping the grep call in length():

  if(length(grep(Str1, Str2)) > 0) { stuff } else { other stuff }

   -Peter Ehlers



More information about the R-help mailing list