Bill.Venables at csiro.au
2008-Apr-06 07:47 UTC
[R] What to use for assignment, " = " or " <- "?
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/
<Bill.Venables <at> csiro.au> writes:> > 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. >[snip]> 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. >It may not be a good reason, but the reason I usually teach "=" rather than "<-" to my students is that they are usually learning scripting/programming for the very first time, and the "=" syntax for assignment (which as I recall was called "gozzinta", for "goes into", in the FORTRAN coloring book) seems natural to most people (even though it's logically quite different). They are so overwhelmed by learning new things that I don't want to add one more. (This is admittedly a judgment call.) Ben Bolker