Message-ID: <Pine.LNX.4.21.0004131205010.16561-100000@pc16.stat.umu.se>
Date: 2000-04-13T10:24:28Z
From: Göran Broström
Subject: Can we get rid of && and ||?
In-Reply-To: <Pine.LNX.3.96.1000413105819.14216B-100000@TILL.math.uni-hamburg.de>
I have some doubts about the implementation of the operators '&&' and
'||'. I only discuss '&&' here. The reason: One of my students tried
(slightly simplified here)
> x <- c(0.5, 1.2, -0.8, 0.7)
> y <- x[(x > 0) && (x < 1)]
> y
[1] 0.5 1.2 -0.8 0.7
This was not his intention. He wanted
> y <- x[(x > 0) & (x < 1)]
> y
[1] 0.5 0.7
I suggested that his first attempt should render a "syntax error", since
'&&' only applies to scalar logicals. However, upon reading
the documentation, I found that 'a && b' is in fact equivalent to
'a[1] & b[1]' (with one exception).
Therefore, '&&' seems to be of little use in R programming. You can
almost always use '&' instead (This is in sharp contrast to the use of
& and && in the C programming language). I can only see one
advantage (admittedly important, though) with '&&'. In
if (is.numeric(x) && min(x) > 0) ..... (Found in MASS3, p. 94)
you avoid 'min(x)' to be evaluated if x is non-numeric. This doesn't
work with '&', because then both expressions are evaluated before the
comparison.
Provided there are no other differences, I suggest that either
a) '&&' is made obsolete, and '&' is 'improved' so that b in
'a & b' is evaluated only if some component of a is TRUE, or
b) a warning (or syntax error) is given if '&&' is used with at least
one non-scalar argument.
Any objections?
G?ran
----------------------------------------------------------
G?ran Brostr?m tel: +46 90 786-5223
Department of Statistics
Ume? University
SE-90187 Ume?, Sweden email: gb at stat.umu.se
----------------------------------------------------------
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._