[R] difference between <- and =
spencer.graves at pdf.com
Mon Sep 1 11:57:20 CEST 2003
A common, punishing error for me, with DF being a data frame, is the
if(DF$a = 1) ...
where I intended to write "if(DF$a == 1)...". This error first
replaces column "a" of DF with the trivial vector 1 (of length 1), and
then interprets that as a logical, which evaluates as TRUE. Unless the
"if" statement otherwise generates an error, I must restore column "a"
from somewhere before I can continue.
In addition to specifying function arguments, I also use "=" to
specify named components of a list or a vector. That works fine for me.
It's only the accidental use of "=" when I mean "==" that creates
Richard A. O'Keefe wrote:
> Levi Larkey <larkey at mail.utexas.edu> wrote:
> a perfect illustration of why allowing "=" as an alternative
> spelling for "<-" was always an obviously really bad idea.
> Is there any reason for using one operator over the other outside of
> function definitions and calls?
> You should never use "=" for assignment unless you are suffering such
> severe "C" withdrawal symptoms that the doctor just had to give you a
> shot to calm you down. It is best to keep different symbols for
> different purposes. (I note that Ada uses ":=" for assignment and
> "=>" for keyword arguments.)
> Something that would be quite useful would be an option asking R to
> warn you
> - if you have any assignment operator in a function argument
> (thanks to lazy evaluation this probably *won't* do what you
> - if you have "=" anywhere except for binding a name to an argument.
> Perhaps these could be two options.
> R-help at stat.math.ethz.ch mailing list
More information about the R-help