[R] the less-than-minus gotcha

Ista Zahn istazahn at gmail.com
Tue Feb 3 03:36:15 CET 2015


On Mon, Feb 2, 2015 at 8:57 PM, Steve Taylor <steve.taylor at aut.ac.nz> 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
>
> ______________________________________________
> 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.



More information about the R-help mailing list