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
Is = now the same as <- in assigning values
17 messages · Jorge Ivan Velez, Gabor Grothendieck, Wacek Kusnierczyk +5 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081215/d9cdc6a5/attachment.pl>
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:
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
______________________________________________ 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.
Petter Hedberg wrote:
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 =.
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:
Petter Hedberg wrote:
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 =.
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
... 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>:
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:
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
______________________________________________ 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.
2 days later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081218/dce7d785/attachment.pl>
Kenn Konstabel wrote:
This is legal but doesn't do what you probably expect -- although documentation for `<-` says the value (returned by <-) is 'value' i.e. whatever is on the right side ...
x<-NULL # just to make sure it's not yet 42
foo <- function(a) a
foo({x = 42}) # no result
Wrong...
bar <- foo({x = 42})
bar
[1] 42 What you're seeing is effectively
foo(invisible(42))
i.e. the result is there, just not printing.
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On 12/18/2008 10:13 AM, Kenn Konstabel wrote:
Hi, On Tue, Dec 16, 2008 at 9:13 AM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
... but this is also legal if you really hate <- :
foo({x = 2})
# assign to x, pass to foo as a
This is legal but doesn't do what you probably expect -- although documentation for `<-` says the value (returned by <-) is 'value' i.e. whatever is on the right side ...
x<-NULL # just to make sure it's not yet 42
foo <- function(a) a
foo({x = 42}) # no result
x
[1] 42
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
If you really hate <-, you should do either
foo({(x=42)}) # or ....
foo({x=42; x}) # or even ...
foo(a=force(x=43))
As for = being more intuitive, my favorite example is x=x+1 (how on earth
can x equal x+1 ??? ... wait, this is an assignment ?#??$@{{!!!).
KK
[[alternative HTML version deleted]]
------------------------------------------------------------------------
______________________________________________ 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081218/ee18521e/attachment.pl>
Kenn Konstabel wrote:
Hi, On Tue, Dec 16, 2008 at 9:13 AM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
... but this is also legal if you really hate <- :
foo({x = 2})
# assign to x, pass to foo as a
This is legal but doesn't do what you probably expect -- although documentation for `<-` says the value (returned by <-) is 'value' i.e. whatever is on the right side ...
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?
x<-NULL # just to make sure it's not yet 42
foo <- function(a) a
foo({x = 42}) # no result
x
[1] 42
If you really hate <-, you should do either
foo({(x=42)}) # or ....
foo({x=42; x}) # or even ...
foo(a=force(x=43))
yes, i think the last one should be the only valid version. unless you invent some more complicated syntax...
As for = being more intuitive, my favorite example is x=x+1 (how on earth
can x equal x+1 ??? ... wait, this is an assignment ?#??$@{{!!!).
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 really hate <-, you should do either
foo({(x=42)}) # or ....
foo({x=42; x}) # or even ...
foo(a=force(x=43))
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:
Kenn Konstabel wrote:
Hi,
On Tue, Dec 16, 2008 at 9:13 AM, Wacek Kusnierczyk <
Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
... but this is also legal if you really hate <- :
foo({x = 2})
# assign to x, pass to foo as a
This is legal but doesn't do what you probably expect -- although
documentation for `<-` says the value (returned by <-) is 'value' i.e.
whatever is on the right side ...
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?
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20081218/2ea5e56f/attachment.pl>
Stavros Macrakis wrote:
On Thu, Dec 18, 2008 at 1:37 PM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
Kenn Konstabel wrote:
...foo({x = 2})
...
This is legal but doesn't do what you probably expect -- although
documentation for `<-` says the value (returned by <-) is 'value' i.e.
whatever is on the right side ...
(just to make clear: here stavros is answering kenn, not me)
What do you expect this to do that is different from what it does, namely assign 2 to x and call foo on the value 2, which is the same as the value of x? As long as foo doesn't do tricks with substitute, all the following should be identical:
<snip>
[kenn] If you really hate <-, you should do either
foo({(x=42)}) # or ....
Why the () nested within the {} ? Either one alone is enough.
these are not identical: x = 1 (x = 1) <snip>
[vQ] 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.
The character string denoting assignment really has no deep importance, whether it's = (Fortran, C); := (Algol, Pascal, Ada); <- (R); : (Maxima); etc. However, using the same symbol to denote two quite different things which can be meaningful in the same contexts, namely assignment and argument naming, strikes me as a poor design decision.
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:
On Thu, Dec 18, 2008 at 1:37 PM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
Kenn Konstabel wrote:
...foo({x = 2})
... This is legal but doesn't do what you probably expect -- although
documentation for `<-` says the value (returned by <-) is 'value' i.e. whatever is on the right side ...
What do you expect this to do that is different from what it does, namely
assign 2 to x and call foo on the value 2, which is the same as the value of
x? As long as foo doesn't do tricks with substitute, all the following
should be identical:
foo(x<-2)
foo((x<-2))
foo((x=2))
foo({x=2})
foo({x<-2})
foo(a=(x=2))
{x=2; foo(invisible(x))}
{x<-2; foo(invisible(x))}
{x=2; foo(invisible(2))}
{x<-2; foo(invisible(2))}
foo(a=x<-2)
.....
Of course, if foo *does* use substitute, all bets are off, because it would capture the argument as an unevaluated expression and could do whatever it wanted with it.
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
O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Stavros Macrakis wrote:
On Thu, Dec 18, 2008 at 1:37 PM, Wacek Kusnierczyk < Waclaw.Marcin.Kusnierczyk at idi.ntnu.no> wrote:
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.
The character string denoting assignment really has no deep importance, whether it's = (Fortran, C); := (Algol, Pascal, Ada); <- (R); : (Maxima); etc. However, using the same symbol to denote two quite different things which can be meaningful in the same contexts, namely assignment and argument naming, strikes me as a poor design decision.
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