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

Bill.Venables at csiro.au Bill.Venables at csiro.au
Sun Apr 6 09:47:45 CEST 2008


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
AUSTRALIA
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
mailto:Bill.Venables at csiro.au
http://www.cmis.csiro.au/bill.venables/ 



More information about the R-help mailing list