[R] the less-than-minus gotcha

Ted Harding Ted.Harding at wlandres.net
Mon Feb 2 13:43:55 CET 2015


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) <Ted.Harding at wlandres.net>
Date: 02-Feb-2015  Time: 12:43:51
This message was sent by XFMail



More information about the R-help mailing list