Re: [R] if statement error

From: Alex Brown <alex_at_transitive.com>
Date: Tue 17 Oct 2006 - 18:51:32 GMT

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@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.



R-help@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. Received on Wed Oct 18 05:08:56 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 17 Oct 2006 - 19:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.