[R] how to make the dynamically creted string work inside if as a condition

Duncan Murdoch murdoch at stats.uwo.ca
Sat Jun 6 13:05:14 CEST 2009


Moumita Das wrote:
> Hi,
> How to make an if condition work,  if the condition inside if() is created
> dynamically  ,and that is a string .If i type teh dynamically created string
> the if works fine but when dynamically created,it is a string and going
> inside the if() ,an error is thrown saying : rgument is not logical ..I even
> tried changing teh mode of the string to logical,but it doesn't work
>
> Say my dynamically created string is :--
>
> str<- name=='tom' || name=='harry' || name=='tina'
> where the names are diffrent for diffrent DBs.
>   

That's not a string, it will be evaluated to a logical TRUE or FALSE value.
> if(name=='tom' || name=='harry' || name=='tina' )
> {
>  some code
> }
>
> If i do this
> if(str)
> {
>
>  some code
> }
>
> It doesn't work:(
>   
Those should be equivalent.  I think you need to simplify your example 
and post real code for us to see the problem.

Duncan Murdoch




More information about the R-help mailing list