[R] What to use for assignment, " = " or " <- "?

From: <Bill.Venables_at_csiro.au>
Date: Sun, 06 Apr 2008 17:47:45 +1000

I've noticed an increasing tendency for people to use '=' rather than the older '<-' symbol. When '=' became available as an assignment operator in S-PLUS in the late '90s my first reaction was to switch to it as well. Brian Ripley warned me that it was not a good idea. As usual he was right, but it took a couple of pretty serious finger-burning episodes before I came fully around to his view.

There are three left assignment operators in S, and it's a good idea to distinguish what they do.

a <<- b

        assigns a value 'b' to an object 'a' in some parent environment

a <- b

        assigns a value 'b' to an object 'a' in the current environment

a = b (inside the argument list of a function call)

        potentially assigns a value 'b' to an object 'a' in the child environment of the function. Lazy evaluation determines if it actually happens or not.

You must use '=' for the third of these. If you choose to use it for the second as well, as is allowed, there is a danger that you will confuse the environment in which the assignment is actually made. It's not a great danger, of course, but it can happen. In any case, it is a good idea to use three separate operators for these three distinct purposes, if nothing else as a clear visual reminder of what kind of assignment is intended to take place.

I suspect the push towards using '=' instead of '<-' has two main drivers:

  1. the world is full of lazy typists
  2. right now there seems to be a big influx of Matlab people into R, and it makes them feel more at home.

Neither of these is much of a reason, I reckon.

Bill Venables
CSIRO Laboratories
PO Box 120, Cleveland, 4163

Office Phone (email preferred): +61 7 3826 7251
Fax (if absolutely necessary):  +61 7 3826 7304
Mobile:                         +61 4 8819 4402
Home Phone:                     +61 7 3286 7700

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 Sun 06 Apr 2008 - 07:50:55 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 Sun 06 Apr 2008 - 15:30:26 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