Skip to content

Is there any difference between <- and =

5 messages · Sean Zhang, Duncan Murdoch, David Smith +2 more

#
On 3/11/2009 10:18 AM, Sean Zhang wrote:
Use <- for assignment, and = for function arguments.  Then the 
difference between

  f( a = 3 )
  f( a <- 3 )

is clear, and you won't be surprised that a gets changed in the second 
case.  If you use = for assignment, the two lines above will be written as

  f( a = 3 )
  f( ( a = 3 ) )

and it is very easy to miss the crucial difference between them.

Duncan Murdoch
#
On Wed, Mar 11, 2009 at 7:18 AM, Sean Zhang <seanecon at gmail.com> wrote:
The short answer is that <- is used for assignment, and = is used to
associate function arguments with their values.  You can use = instead
of <- for assignment too (in most contexts), but the converse isn't
true.

I've provided more detail about when you can and can't exchange the
two operators (and some of the history about the operators themselves)
in this blog post:

http://blog.revolution-computing.com/2008/12/use-equals-or-arrow-for-assignment.html

# David Smith
#
Duncan Murdoch wrote:
in fact, some recent posts show that things can go the other way round: 
people try to use <- for function arguments.  i think the following is
the most secure way if one really really has to do assignment in a
function call:

    f({a=3})

and if one keeps this convention, <- can be dropped altogether.

vQ
1 day later
#
I think most people find it odd at first if they have always used "=" but
quickly you get use to it and nothing could be more clear. It is explicit.
It is active and provides a direction, a value goes into an object. The
equal sign for assignment is ambiguous. 

As an example 

 x = 3

we only know that the value 3 is assigned to the object x because by
convention a number cannot be an object, if not it could be read as the
object "3" taking the value "x" The expression literally states that they
are equal, yet you cannot assume that all instances of 3 are equal to x, so
it is an inaccurate expression. On the other hand,

3 -> x  or 
x <- 3  

is very clear. It makes no changes to "3" only to "x"  I've been reading
"Data Manipulation with R" and find the author's use of "=" for assignment
disturbing. You quickly get use to -> and will find after a short time that
you prefer it.
Sean Zhang wrote: