All the more reason to use = instead of <- -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker Sent: Monday, 2 February 2015 2:07p To: r-help at stat.math.ethz.ch Subject: Re: [R] the less-than-minus gotcha Mike Miller <mbmiller+l <at> gmail.com> writes:> > I've got to remember to use more spaces. Here's the basic problem: > > These are the same: > > v< 1 > v<1 > > But these are extremely different: > > v< -1 > v<-1 >This is indeed documented, in passing, in one of the pages you listed: http://tim-smith.us/arrgh/syntax.html Whitespace is meaningless, unless it isn't. Some parsing ambiguities are resolved by considering whitespace around operators. See and despair: x<-y (assignment) is parsed differently than x < -y (comparison)! ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Using = has it's problems too. For example, print(fit <- lm(...)) Assigns the result of the lm call to fit and prints the results. This is quite a useful trick actually. print(fit = lm(...)) Throws an error. Moral of story, computers do what you tell them, not what you meant. Kevin On 02/01/2015 08:26 PM, Steve Taylor wrote:> All the more reason to use = instead of <- > > > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker > Sent: Monday, 2 February 2015 2:07p > To: r-help at stat.math.ethz.ch > Subject: Re: [R] the less-than-minus gotcha > > Mike Miller <mbmiller+l <at> gmail.com> writes: > >> >> I've got to remember to use more spaces. Here's the basic problem: >> >> These are the same: >> >> v< 1 >> v<1 >> >> But these are extremely different: >> >> v< -1 >> v<-1 >> > > This is indeed documented, in passing, in one of the pages you listed: > > http://tim-smith.us/arrgh/syntax.html > > Whitespace is meaningless, unless it isn't. Some parsing ambiguities > are resolved by considering whitespace around operators. See and > despair: x<-y (assignment) is parsed differently than x < -y (comparison)! >-- Kevin E. Thorpe Head of Biostatistics, Applied Health Research Centre (AHRC) Li Ka Shing Knowledge Institute of St. Michael's Assistant Professor, Dalla Lana School of Public Health University of Toronto email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016
"Moral of story, computers do what you tell them, not what you meant." Not only a fortune, but profound wisdom. Jim On Mon, Feb 2, 2015 at 12:38 PM, Kevin E. Thorpe <kevin.thorpe at utoronto.ca> wrote:> Using = has it's problems too. > > For example, > > print(fit <- lm(...)) > > Assigns the result of the lm call to fit and prints the results. This is > quite a useful trick actually. > > print(fit = lm(...)) > > Throws an error. > > Moral of story, computers do what you tell them, not what you meant. > > Kevin > > On 02/01/2015 08:26 PM, Steve Taylor wrote: >> >> All the more reason to use = instead of <- >> >> >> -----Original Message----- >> From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker >> Sent: Monday, 2 February 2015 2:07p >> To: r-help at stat.math.ethz.ch >> Subject: Re: [R] the less-than-minus gotcha >> >> Mike Miller <mbmiller+l <at> gmail.com> writes: >> >>> >>> I've got to remember to use more spaces. Here's the basic problem: >>> >>> These are the same: >>> >>> v< 1 >>> v<1 >>> >>> But these are extremely different: >>> >>> v< -1 >>> v<-1 >>> >> >> This is indeed documented, in passing, in one of the pages you listed: >> >> http://tim-smith.us/arrgh/syntax.html >> >> Whitespace is meaningless, unless it isn't. Some parsing ambiguities >> are resolved by considering whitespace around operators. See and >> despair: x<-y (assignment) is parsed differently than x < -y (comparison)! >> > > > -- > Kevin E. Thorpe > Head of Biostatistics, Applied Health Research Centre (AHRC) > Li Ka Shing Knowledge Institute of St. Michael's > Assistant Professor, Dalla Lana School of Public Health > University of Toronto > email: kevin.thorpe at utoronto.ca Tel: 416.864.5776 Fax: 416.864.3016 > > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
On 02/02/15 14:26, Steve Taylor wrote:> All the more reason to use = instead of <-Couldn't agree less, Steve. The "<-" should be used for assignment. The "=" sign should be reserved for handling function arguments in the "name=value" form. Doing anything else invites confusion and occasionally chaos. Lots of examples have been given in the past involving syntax of the form foo(x = y) and foo(x <- y). cheers, Rolf -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619
I'd rather say that it is good reason to use spaces around operators. Makes the code easier to read too. (_Possible_ to read for some.) We've probably all done it. I did, once upon a time in S-Plus, on the results of a multi-day simulation study:> <<nontrivial analysis>>$statistic[1] -1.28> sim <- sapply(1:10000, <<simulated nontrivial analysis>>$statistic). . leave running .> mean(sim<-1.28) # get empirical p-value[1] 1.28 ??!?... Uh-oh..... On 02 Feb 2015, at 02:26 , Steve Taylor <steve.taylor at aut.ac.nz> wrote:> All the more reason to use = instead of <- > >-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On 02-Feb-2015 11:58:10 Rolf Turner wrote:> > On 02/02/15 14:26, Steve Taylor wrote: > >> All the more reason to use = instead of <- > > Couldn't agree less, Steve. The "<-" should be used for assignment. The > "=" sign should be reserved for handling function arguments in the > "name=value" form. Doing anything else invites confusion and > occasionally chaos. > > Lots of examples have been given in the past involving syntax of the > form foo(x = y) and foo(x <- y). > > cheers, > Rolf > -- > Rolf TurnerAs well as agreering with Rolf, it should also be said that Steve's advice "All the more reason to use = instead of <-" is not applicable in this context, which was: which( frame$var>4 ) # no problem which( frame$var<-4 ) # huge problem: frame$var is destroyed There is no place for an "=" here! What does not seems to have been mentioned so far is that this kind of thing can be safely wrapped in parentheses: which( frame$var>4 ) # no problem oper which( frame$var<(-4) ) # [again no problem] Personally, I'm prone to using parentheses if I have any feeling that a "gotcha" may be lurking -- not only in the distinction between "var<-4" and "var< -4", but also to be confident that, for instance, my intentions are not being over-ridden by operator precedence rules. Some people object to code "clutter" from parentheses that could be more simply replaced (e.g. "var< -4" instead of "var<(-4)"), but parentheses ensure that it's right and also make it clear when one reads it. Best wishes to all, Ted. ------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at wlandres.net> Date: 02-Feb-2015 Time: 12:43:51 This message was sent by XFMail
Rolf Turner is right on the money about not mixing-up '=' and '<-' Though this 'gotcha' will always a threat while '<-' is the assignment operator. The old Algol60 syntax of ':=' was less error-prone, but I guess '<-' is too firmly bedded-in to ever change. Meanwhile, spaces around the assignment operator gives less error-prone and more readable code. cheers Bob Kinley _____________________________________________ -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Rolf Turner Sent: 02 February 2015 11:58 To: Steve Taylor; r-help at stat.math.ethz.ch Subject: Re: [R] the less-than-minus gotcha On 02/02/15 14:26, Steve Taylor wrote:> All the more reason to use = instead of <-Couldn't agree less, Steve. The "<-" should be used for assignment. The "=" sign should be reserved for handling function arguments in the "name=value" form. Doing anything else invites confusion and occasionally chaos. Lots of examples have been given in the past involving syntax of the form foo(x = y) and foo(x <- y). cheers, Rolf -- Rolf Turner Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276 Home phone: +64-9-480-4619 ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
> All the more reason to use = instead of <-Definitely not! (As you were told, there are other drawbacks). R does not have to look like C, it *is* different in many ways. If you use a decent IDE for R, you get spaces around ' <- ' for free: Both in ESS and in Rstudio, you can use "[Alt] -" to produce the 4 characters ' <- ' { [Alt] + "-") is called 'M--' in ESS / emacs which has even more options for " <- " and is fully configurable in its key bindings anyway. } The '=' character has many uses in R and using ' <- ' for assignment makes the code "more expressive": It makes sense to highlight the assignment op, but is a bit stupid to highlight all "=" signs. Further it can be nicely marked up by a real "left arrow" by e.g. the listings LaTeX 'listings' package, or the (oldish) 'a2ps' GNU software. Further, assignment is not commutative, and hence, there is a corresponding ` -> ` operator, whereas the '=' is a commutative operator in mathematics, but not when used as assignment op. [ yes: "Flame war is on. I'll stop reading R-help for a while.." ;-) ;-) ]> -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker > Sent: Monday, 2 February 2015 2:07p > To: r-help at stat.math.ethz.ch > Subject: Re: [R] the less-than-minus gotcha> Mike Miller <mbmiller+l <at> gmail.com> writes:> > > > I've got to remember to use more spaces. Here's the basic problem: > > > > These are the same: > > > > v< 1 > > v<1 > > > > But these are extremely different: > > > > v< -1 > > v<-1 > >> This is indeed documented, in passing, in one of the pages you listed:> http://tim-smith.us/arrgh/syntax.html> Whitespace is meaningless, unless it isn't. Some parsing ambiguities > are resolved by considering whitespace around operators. See and > despair: x<-y (assignment) is parsed differently than x < -y (comparison)!
Responding to several messages in this thread...> > All the more reason to use = instead of <- > Definitely not!Martin and Rolf are right, it's not a reason for that; I wrote that quickly without thinking it through. An "=" user might be more likely to fall for the gotcha, if not spacing their code nicely. So the lesson learned from the gotcha is that it's good to space your code nicely, as others have siad, not which assignment symbol to use. However, I continue to use "=" for assignment on a daily basis without any problems, as I have done for many years. I remain unconvinced by any and all of these arguments against it in favour of "<-". People telling me that I "should" use the arrow need better agruments than what I've seen so far. I find "<-" ugly and "->" useless/pointless, whereas "=" is simpler and also nicely familiar from my experience in other languages. It doesn't matter to me that "=" is not commutative because I don't need it to be.> Further it can be nicely marked up by a real "left arrow" > by e.g. the listings LaTeX 'listings' package...Now that's just silly, turning R code into graphical characters that are not part of the R language.> foo(x = y) and foo(x <- y)I'm well aware of this distinction and it never causes me any problems. The latter is an example of bad (obfuscated) coding, IMHO; it should be done in two lines for clarity as follows: x = y foo(x)> Using = has it's problems too.Same goes for apostrophes. Shall we discuss putting "else" at the start of line next? cheers, Steve