the less-than-minus gotcha

classic Classic list List threaded Threaded
18 messages Options
Reply | Threaded
Open this post in threaded view
|

the less-than-minus gotcha

Mike Miller-13
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 mistake can get you even inside of a function call like this:

> v <- -2:2
> which( v<1 )
[1] 1 2 3
> which( v<-1 )  # oops, I meant v< -1 not v<-1 a HUGE mistake!
Error in which(v <- 1) : argument to 'which' is not logical
> v
[1] 1

It throws an error but not because I just destroyed my data.  R has no way
of knowing that I didn't intend to overwrite the vector.

This was how it got me:

which( frame$var>4 )   # no problem

which( frame$var<-4 )  # huge problem: frame$var is destroyed

Too late now:  The data in frame$var were all overwritten with 4s.  So the
data are lost and might not be recoverable.

Maybe the tactic that will save me in the future is to remember to always
use plenty of spaces:

which( frame$var > 4 )

which( frame$var < -4 )

That also makes the code easier to read.  In a script, I probably would
have done that, but in an interactive session I can be lazy.

It seems that there are a lot of "R gotcha" pages on the web but quite a
few of the examples show R behaving exactly like I would want and expect
(e.g., of course NA + 5 returns NA)...

https://github.com/mikelove/r-gotchas/blob/master/README.md
http://stackoverflow.com/questions/1535021/whats-the-biggest-r-gotcha-youve-run-across
http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
http://biostat.mc.vanderbilt.edu/wiki/Main/LinuxWorkshopRProgramingTipsAndGotchas
http://tim-smith.us/arrgh/

I didn't happen to see my example on any list, but I didn't read them
thoroughly, so it's probably there.

Mike

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

bbolker
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)!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Steve Taylor
All the more reason to use = instead of <-


-----Original Message-----
From: R-help [mailto:[hidden email]] On Behalf Of Ben Bolker
Sent: Monday, 2 February 2015 2:07p
To: [hidden email]
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)!

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Kevin E. Thorpe
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:[hidden email]] On Behalf Of Ben Bolker
> Sent: Monday, 2 February 2015 2:07p
> To: [hidden email]
> 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: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Jim Lemon-4
"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
<[hidden email]> 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:[hidden email]] On Behalf Of Ben Bolker
>> Sent: Monday, 2 February 2015 2:07p
>> To: [hidden email]
>> 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: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016
>
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Rolf Turner
In reply to this post by Steve Taylor

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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Peter Dalgaard-2
In reply to this post by Steve Taylor
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 <[hidden email]> 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Ted Harding
In reply to this post by Rolf Turner
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 Turner

As 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) <[hidden email]>
Date: 02-Feb-2015  Time: 12:43:51
This message was sent by XFMail

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Robert Douglas Kinley
In reply to this post by Rolf Turner

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:[hidden email]] On Behalf Of Rolf Turner
Sent: 02 February 2015 11:58
To: Steve Taylor; [hidden email]
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

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Martin Maechler-5
In reply to this post by Steve Taylor

> 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:[hidden email]] On Behalf Of Ben Bolker
> Sent: Monday, 2 February 2015 2:07p
> To: [hidden email]
> 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)!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Steve Taylor
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Ista Zahn
On Mon, Feb 2, 2015 at 8:57 PM, Steve Taylor <[hidden email]> wrote:
> 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.

Fair enough, but you skipped right past the most important one: it
makes code easier to read. It's very nice to be able to visually scan
through the code and easily see where assignment happens.

>
> I find "<-" ugly and "->" useless/pointless,

I never used it either, until recently when I started using magrittr.
Maybe still pointless, but I find the form
> library(magrittr)
>
> "hello" %>%
+     paste("world,") %>%
+     paste("my name is") %>%
+     paste("Ista") -> hi
> hi
[1] "hello world, my name is Ista"

quite nice.

--Ista

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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Steve Taylor
I disagree.  Assignments in my code are all lines that look like this:

variable = expression

They are easy to find and easy to read.

-----Original Message-----
From: Ista Zahn [mailto:[hidden email]]
Sent: Tuesday, 3 February 2015 3:36p
To: Steve Taylor
Cc: [hidden email]
Subject: Re: [R] the less-than-minus gotcha

On Mon, Feb 2, 2015 at 8:57 PM, Steve Taylor <[hidden email]> wrote:
Fair enough, but you skipped right past the most important one: it
makes code easier to read. It's very nice to be able to visually scan
through the code and easily see where assignment happens.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Jeff Newmiller
In reply to this post by Steve Taylor
I did not start out liking <-, but I am quite attached to it now, and even Rcpp feels weird to me now. This may seem like yet another variation on a theme that you don't find compelling, but I find that

f(x=x)

makes sense when scope is considered, but

x=x

on its own is silly. That is why I prefer to reserve = for assigning parameters... I use it to clarify that I am crossing scope boundaries, while <- never does. (<<- is a dangerous animal, though... to be used only locally in nested function definitions).

In my view, this is similar to preferring == from C-derived syntaxes over the overloaded = from, say, Basic. I am sure you can get by with the syntactic overloading, but if you have the option of reducing ambiguity, why not use it?

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On February 2, 2015 5:57:05 PM PST, Steve Taylor <[hidden email]> wrote:

>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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Steve Taylor
Nobody would write x=x or indeed x<-x; both are silly.  If I found myself writing f(x=x) I might smirk at the coincidence, but it wouldn't bother me.  I certainly wouldn't confuse it with assigning x to itself.

By the way, here's another assignment operator we can use:

`:=` = `<-`  # this is going in my .Rprofile
x := 1


-----Original Message-----
From: Jeff Newmiller [mailto:[hidden email]]
Sent: Tuesday, 3 February 2015 3:54p
To: Steve Taylor; [hidden email]
Subject: Re: [R] the less-than-minus gotcha

I did not start out liking <-, but I am quite attached to it now, and even Rcpp feels weird to me now. This may seem like yet another variation on a theme that you don't find compelling, but I find that

f(x=x)

makes sense when scope is considered, but

x=x

on its own is silly. That is why I prefer to reserve = for assigning parameters... I use it to clarify that I am crossing scope boundaries, while <- never does. (<<- is a dangerous animal, though... to be used only locally in nested function definitions).

In my view, this is similar to preferring == from C-derived syntaxes over the overloaded = from, say, Basic. I am sure you can get by with the syntactic overloading, but if you have the option of reducing ambiguity, why not use it?

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

On February 2, 2015 5:57:05 PM PST, Steve Taylor <[hidden email]> wrote:

>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
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Mike Miller-13
Kevin Thorpe wrote,

"Moral of story, computers do what you tell them, not what you meant."

But hope springs eternal!  Of course this aphorism explains neatly every
problem I've ever run across while using a computer.  But maybe someday
they'll make a computer that undertands *me*!

Peter Dalgaard wrote,

"I'd rather say that it is good reason to use spaces around operators.
Makes the code easier to read too."

I think that's the real lesson.  I try to do that, but I'll be trying even
harder now.


If they'd thought of it long ago, S and R could have been written so that
"<-" had to be followed by a space or an error would result.  That might
have been a good idea, but it's too late now.  So we just have to not make
mistakes.  I'm working on it.

Mike

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

Martin Maechler-5
In reply to this post by Steve Taylor
>>>>> Steve Taylor <[hidden email]>
>>>>>     on Tue, 3 Feb 2015 03:11:47 +0000 writes:

    > Nobody would write x=x or indeed x<-x; both are silly.  If
    > I found myself writing f(x=x) I might smirk at the
    > coincidence, but it wouldn't bother me.  I certainly
    > wouldn't confuse it with assigning x to itself.  By the
    > way, here's another assignment operator we can use:

    > `:=` = `<-` # this is going in my .Rprofile
    >  x := 1

I very much agree with you on that last point.

A while ago, about or shortly before when R was created, say ca. 1991-1993,
when S4 was "around the corner", and I was a beta tester for it,
and  '=' was going to be introduced into the S language,
I had a fervent plea for considering  ":="  as an alternative...
... but was never even replied to IIRC.

The fact that R's parser allows  ':='  is an interesting
historic relict itself.
Before R even got a version number, R did not have packages yet,
there was "kind of a distinction" (don't ask me for details now)
between "base R functions" and "other R objects"
 {If Ross Ihaka reads this, he will cringe I'm sure.. sorry, Ross!}
and all those used :=

The last version that still used this extensively 0.13, is still available
as  R-0.13alpha.tar.gz  (813570 bytes, dated  Nov 7, 1996),
and contains a file src/library/base/median  (note, no 'R'
subdirectory) which looks like this
---------------------------------------------------------------------
median := function(x, na.rm = FALSE) {
        if(na.rm)
                x <- x[!is.na(x)]
        else if(any(is.na(x)))
                return(NA)
        n <- length(x)
        half <- (n + 1)/2
        if(n %% 2 == 1) {
                sort(x, partial = half)[half]
        }
        else {
                sum(sort(x, partial = c(half, half + 1))[c(half, half + 1)])/2
        }
}
---------------------------------------------------------------------

so yes, ":="  had its nice history,  but as it was not accepted
into S  *instead* of '=', it also fell away for R ...

Martin Maechler,
ETH Zurich  (and R Core team since "prehistory")


    > -----Original Message----- From: Jeff Newmiller
    > [mailto:[hidden email]] Sent: Tuesday, 3
    > February 2015 3:54p To: Steve Taylor;
    > [hidden email] Subject: Re: [R] the
    > less-than-minus gotcha

    > I did not start out liking <-, but I am quite attached to
    > it now, and even Rcpp feels weird to me now. This may seem
    > like yet another variation on a theme that you don't find
    > compelling, but I find that

    > f(x=x)

    > makes sense when scope is considered, but

    > x=x

    > on its own is silly. That is why I prefer to reserve = for
    > assigning parameters... I use it to clarify that I am
    > crossing scope boundaries, while <- never does. (<<- is a
    > dangerous animal, though... to be used only locally in
    > nested function definitions).

    > In my view, this is similar to preferring == from
    > C-derived syntaxes over the overloaded = from, say,
    > Basic. I am sure you can get by with the syntactic
    > overloading, but if you have the option of reducing
    > ambiguity, why not use it?

    > ---------------------------------------------------------------------------
    > Jeff Newmiller The .....  .....  Go Live...
    > DCN:<[hidden email]> Basics: ##.#.  ##.#.  Live
    > Go...  Live: OO#.. Dead: OO#..  Playing Research Engineer
    > (Solar/Batteries O.O#.  #.O#.  with /Software/Embedded
    > Controllers) .OO#.  .OO#.  rocks...1k
    > ---------------------------------------------------------------------------
    > Sent from my phone. Please excuse my brevity.

    > On February 2, 2015 5:57:05 PM PST, Steve Taylor
    > <[hidden email]> wrote:
    >> 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
    >>
    >> ______________________________________________
    >> [hidden email] 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.

    > ______________________________________________
    > [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: the less-than-minus gotcha

glsnow
In reply to this post by Steve Taylor
Steve (and any others still paying attention to this thread),

Larry Wall (author of Perl) said something along the lines of:

things that are similar should look similar, things that are different
should look different.

Ironically one of the first places I saw that quote was in a Perl vs.
Python language war and the same quote was being used by both sides.  This
made me think that you can learn a lot about how programmers think by what
they make look similar and what they make look different (and therefore
what they think are similar and different).

In R, I see the assignments:

foo( x <- something )

as assign to the variable x in the current (or calling) environment and

foo( x = something )

as assign to the variable x in the environment created by the function call.

With this distinction only "<-" makes any sense at the top level, but then
I see assigning to the 2 different environments as 2 different things that
should look different.  Using "=" for assignment makes sense if (and only
if) you consider assigning into different environments to be the same (or
at least very similar) thing.


Also you mentioned that

foo( x <- something )

should always be written as:

x <- something
foo(x)

and for many cases I agree, but consider the counter example of:

system.time( out <- longRunningSimulationFunction(n=1e6) )

compared to:

out = longRunningSimulationFunction(n=1e6)
system.time(out)

I would argue that the 1st version is much more useful than the 2nd.


On Mon, Feb 2, 2015 at 6:57 PM, Steve Taylor <[hidden email]> wrote:

> 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
>
> ______________________________________________
> [hidden email] 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.
>



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.