Re: [R] Case statements in R

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat, 02 Aug 2008 11:04:27 +0200

(Ted Harding) wrote:
> On 28-Jul-08 17:52:31, Henrik Bengtsson wrote:
>
>> Use '&' for vectors and '&&' for scalars. Ditto applies to the OR
>> operator(s). /Henrik
>>
>
> What's wrong with using "&" for scalars? Surely it gives the
> correct answer? Maybe it's simply a bit slower, or something?
>
Semantics are subtly different. a && b does not evaluate b if a is FALSE (it is equivalent to if(a) then b else FALSE), so

 > F & print("Hi")
[1] "Hi"
Error in F & print("Hi") :
  operations are possible only for numeric or logical types  > F && print("Hi")
[1] FALSE This is a bit synthetic, but it shows that there are differences in side effects and error checking. In real life, an important case would be when "a" is a check that "b" is at all computable.

     -p

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help_at_r-project.org 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 Sat 02 Aug 2008 - 09:12:04 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 02 Aug 2008 - 09:33:02 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.

list of date sections of archive