[R] Case statements in R

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sat Aug 2 11:04:27 CEST 2008


(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



More information about the R-help mailing list