[R] if statement error

Alex Brown alex at transitive.com
Tue Oct 17 20:51:32 CEST 2006


On 17 Oct 2006, at 18:34, Alberto Monteiro wrote:

> Jenny Stadt wrote:
>>
>> I was not able to make this work. I know it is a simple one, sorry
>> to bother. Give me some hints pls. Thanks!
>>
> Are you a C programmer? :-)
>
>> if(length(real.d)>=30 && length(real.b)>=30 &&
>> beta1*beta2*theta1*theta2>0 )
>>
>> { r <- 1;  corr <- 1;  }
>>
> I _think_ you should use & instead of &&. And drop the second ";".
>

The && is correct in this case.
& is the vector logical AND operator in R (and analogously the  
bitwise logical AND in C)
&& is the lazy scalar (atomic) logical AND operator in C and R.  If  
it operates on a vector in R, it ignores all but the first element.   
see help("&&")
since if() in R is scalar (atomic) the && is appropriate.

The second ';' is syntactically correct in R and C, although optional  
in R.

-Alex

Out of interest, for a vector equivalent to if, see help(ifelse)

> Also, don't forget that "return x" is wrong [it took me a long
> time to figure out that R != C, and it's just return(x)]
>
> Alberto Monteiro
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list