Skip to content

Is = now the same as <- in assigning values

17 messages · Jorge Ivan Velez, Gabor Grothendieck, Wacek Kusnierczyk +5 more

#
I?m a PhD student at the University of Warsaw, and have started using R.
In many books they specify to use <- instead of = when assigning
values, and this is also mentioned in older posts on the R website.

However, it seams to me that some update has occured, becuase I
continously get the same result wether I use <- or =.

I would be extremely helpful for any answer to this.
= seams more intuitive, so I assumed that an update had been made due
to popular demand and that was why I get the same output wether I use
<- or =.

Best regards,

Petter Hedberg
#
In most cases <- and = are the same yet its not always
true so its safest to use <- for assignment.

Check this out:

http://tolstoy.newcastle.edu.au/R/e4/help/08/06/12940.html
On Mon, Dec 15, 2008 at 4:26 PM, Petter Hedberg <ekologkonsult at gmail.com> wrote:
#
Petter Hedberg wrote:
czesc,

<- and = are not exactly equivalent.  while in many situations you may
use = instead of <-, in some you can't:

x <- 1
x = 1
# same thing

foo = function(a) ...

foo(x <- 2)
# assign to x, and pass it to foo as the argument a

foo(x = 2)
# pass 2 to foo as the argument x


personally, i prefer = to <- wherever possible, but some on this list
have expressed the opinion that such code is rather annoying.  it seems
to be a matter of taste, but you may want to stay in the mainstream and
get used to <-.

vQ
#
Wacek Kusnierczyk wrote:
... but this is also legal if you really hate <- :

foo({x = 2})
# assign to x, pass to foo as a

vQ
#
Thank you all for the reply. I?ll start using <-.

Best regards,

Petter Hedberg
University of Warsaw.


2008/12/16 Gabor Grothendieck <ggrothendieck at gmail.com>:
2 days later
#
Kenn Konstabel wrote:

            
Wrong...
[1] 42


What you're seeing is effectively
i.e. the result is there, just not printing.
#
On 12/18/2008 10:13 AM, Kenn Konstabel wrote:
All of x = 42, x <- 42, foo({x = 42}) and foo(x <- 42) do return 42, but 
they do it "invisibly", so it won't print.  So "no result" isn't quite 
right...

Duncan Murdoch
#
Kenn Konstabel wrote:
as far as i can see, this does precisely what i expect -- it assigns 2
to x and then passes x as the argument a to foo.  did you mean there is
something else happening here?
yes, i think the last one should be the only valid version.  unless you
invent some more complicated syntax...
who said = is more intuitive for assignments?  i said i prefer it, and
that's because of aesthetics, silly me.  in an earlier post, someone
said it is more natural for his students [1].  argue to the contrary.

it depends on how you program, mostly.  if you're doing functional
programming with no reassignments, = is just perfect.

how on earth can x equal x+1?  there is a solution, guess yourself.  but
in r, the semantics of 'x = x + 1'is *not* that x equals x + 1, so
where's the problem.  in a language where = means comparison (e.g., f#),
the expression evaluates to false, no problem.  in a language where =
means unification (e.g., oz), the expression gives a unification
failure.  but there is a long tradition in programming languages of
using = for assignment -- e.g., in fortran.

but even where = means comparison, x = x + 1 may actually evaluate to
true, just as x = x may evaluate to false.  (have you never seen x == x
evaluate to false in java or c, for example?)

vQ



[1] http://tinyurl.com/4o4ha4
#
Kenn Konstabel wrote:
if you think the use of force guarantees that x is assigned 43, you're
wrong.

foo = function(a) 0
x = 1
foo(a=force(x=2))
x

foo = function(a) deparse(substitute(a))
x = 1
foo(a=force(x=2))
x

in both cases, the result (the final value of x, but not the value
returned by foo in the second example) does not change when you replace
'force(x=2)' with 'x<-2'.

vQ
#
Wacek Kusnierczyk wrote:
too quick this time:  the 'then' is not appropriate, as it is an
expression, not its value, that is passed to foo.  so this does what i
expect, and what i'd expect with x <- 2 instead, namely:  x is assigned
2 on the first occasion the argument a is used within foo, and
effectively a has the value of x after the assignment.


vQ
#
Stavros Macrakis wrote:
(just to make clear:  here stavros is answering kenn, not me)
<snip>
these are not identical:

x = 1
(x = 1)


<snip>
as i said, my choice is based on aesthetics, silly me.  i find many
enough poor design decisions in r not be concerned with this superficial
detail.

vQ
#
Stavros Macrakis wrote:
.....
Also watch out for lazy evaluation. It is required that a is evaluated 
inside foo to have equivalence with the pre-assigning forms (and even 
so, things may happen in different order).

Consider

 > foo <- function(a){cat("hello, "); a}
 > foo(cat("world\n"))
hello, world
#
Stavros Macrakis wrote:
you mean foo(x=1) which could be taken both as passing 1 into x or as
assigning 1 to x and passing as the first to foo?  what's poor design is
to even consider the latter in your code.  for reasons you probably know
performing side effects in subexpressions is not a good idea, and if you
have good practices, the poor design decision you mention above is an
artificial problem.

vQ