behavior of =
Ben Bolker
bolker@zoo.ufl.edu
Fri, 8 Nov 2002 09:05:42 -0500 (EST)
I probably didn't follow the discussion of allowing "=" as an assignment
operator closely enough, but I was a little bit horrified to discover
today (using 1.6.0; I haven't upgraded to 1.6.1 yet) that
x <- runif(20)
y <- 1:20
y[x=min(x)]
gives numeric(0) (because min(x) is non-integer).
x <- sample(1:20,20,TRUE)
y[x=min(x)]
is even worse -- it gives the "wrong" answer. I know exactly what's going
on here (the = in the subsetting statement above was a typo for ==), and I
should have known better, but I felt like I was programming in C again!
I know that the designers tried to build in safeguards (from the man
page):
the `=' is only allowed at the top level (that is, in the complete
expression typed by the user) or as one of the subexpressions in a
braced list of expressions.
These safeguards don't prevent the mishap shown above. Is there any way
to detect this case syntactically (check for assignments inside subsetting
operations??), or will I just have to train my students to watch out for
this possibility?
----
318 Carr Hall bolker@zoo.ufl.edu
Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker
Box 118525 (ph) 352-392-5697
Gainesville, FL 32611-8525 (fax) 352-392-3704
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel 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-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._