12

It is a frequent R gotcha that 1:10+5 does not do what you expect it do. All of us R programmers have been bitten by it, usually several times.

Surprisingly, although it comes up a lot, we don't seem to have any canonical questions, or even any well-titled questions. Which should we use, and what title should it have?

22
  • 4
    Once you find one, Send a PR to HammeR. Thanks :) Dec 4, 2016 at 19:09
  • 1
    I wish more language designers had the courage to just make mixing infix operators from different families an error. It's so little extra effort to write either 1:(10+5) or (1:10)+5 and remove the ambiguity.
    – Ben Voigt
    Dec 4, 2016 at 19:10
  • 1
    Additionally, a canonical answer ought to contrast the behavior with that of MATLAB, where 1:i+10 behaves as 1:(i+10).
    – Ben Voigt
    Dec 4, 2016 at 19:15
  • @BenVoigt: probably that should be a separate question, e.g. "What are language gotchas between R and MATLAB?"
    – smci
    Dec 4, 2016 at 19:23
  • 9
    @smci oh no, that would be far too broad. It's entirely appropriate for an answer about precedence to note that it is different in some other languages and list a couple examples. Missing one would not make such an answer wrong, the way missing a gotcha would in your suggestion (which is why questions of the type you proposed are explicitly offtopic)
    – Ben Voigt
    Dec 4, 2016 at 19:41
  • 1
    This seems to be more suitable for the Documentation area than as a question and answer. Dec 5, 2016 at 0:33
  • @MatthewLundberg: no, it's also on-topic here: we need a canonical question. There are already too many historical duplicates. I want to know why their titles all suck, so I can write a canonical one with a good title.
    – smci
    Dec 5, 2016 at 4:02
  • Obviously there are differing opinions on that. Dec 5, 2016 at 4:13
  • 3
    How about "Why doesn't the sequence 1:n + k run like 1, 2, ..., n + k?" Anyway, when you've posted your canonical Q&A, you can/should post an answer on this meta so we can find it. Personally, I think it makes more sense in Q&A than Docs.
    – Frank
    Dec 5, 2016 at 19:18
  • @Frank: usually users don't isolate that it's the : operator behaving differently to what they would visually expect, or what's standard in other languages. I'd call it "Why did an expression with m:n not behave as expected?"
    – smci
    Dec 5, 2016 at 19:43
  • But that doesn't have a + or - in it, right? m:n behaves the same in MATLAB and R. Anyway, I guess another idea would be: if you think there might be multiple ways to approach this Q&A, you can post them as answers below and solicit votes and other feedback to help you decide.
    – Frank
    Dec 5, 2016 at 19:47
  • Yeah, sure, something like "Why did an expression with m:n+k not behave as expected?"
    – smci
    Dec 5, 2016 at 20:38
  • Do we really need a Q/A for a RTFM question ? Handling operator precedence sounds blatantly RTFM for me.
    – Tensibai
    Jan 4, 2017 at 16:24
  • And preaching for myself (feel free to turn it CW if you feel it's needed): this one explain it
    – Tensibai
    Jan 4, 2017 at 16:26
  • 3
    And re "even any well-titled questions": there will never be, as no one think about operator precedence when asking about a strange behavior when using ranges, so obvisouly the questions will never speak about : or operator precedence.
    – Tensibai
    Jan 4, 2017 at 16:29

1 Answer 1

-1

My suggestion: ask a question about it with whatever title you think is best. Then supply a community wiki answer and accept it.

7
  • Among other things, I'm asking you all what title is best?
    – smci
    Dec 4, 2016 at 23:37
  • 4
    Why do you believe such a question/answer pair is needed? This issue is clearly explained in "An Introduction to R" (section 2.3), which everyone who uses R should have read or suffer the consequences. Operator precedence is also detailed in help("Syntax").
    – Roland
    Dec 5, 2016 at 8:00
  • 2
    @Roland I don't really, but SO has decision-making procedures (ie voting) that - better than a meta convo - can decide whether something is worth being a question and procedures (ie editing) that can decide what that question should be titled.
    – Thomas
    Dec 5, 2016 at 9:04
  • 2
    @Roland, that's exactly the attitude among R users that the language can arbitrarily change operator semantics or keywords from what is standard in other languages, and as long as it's buried somewhere in the documentation (or a third-party guide like R-inferno), that we can't be more helpful to users. Gotchas should be documented (R differs from Matlab, Python/pandas, and others), and in this case a canonical question is needed, with a generic title, and to merge all the exact duplicates I listed into it. To prevent wasting time. (Have you never encountered a gotcha in another language?)
    – smci
    Dec 5, 2016 at 19:40
  • You are entitled to your opinion. I do not agree that official documentation needs to be replicated on SO. If you make assumptions instead of checking documentation you deserve what you get.
    – Roland
    Dec 5, 2016 at 19:59
  • 1
    @Roland: I never said "official documentation needs to be replicated on SO", you're misrepresenting me. I did say "major gotchas, esp. where R differs arbitrarily from standard or expected behavior in other similar languages, deserve to get a canonical question"*. I don't make assumptions, I read documentation. This still doesn't excuse arbitrarily differing from standard semantics without calling users' attention to it prominently (list the gotchas). In the case of m:n+k, it's not like raising : precedence over arithmetic buys us any special functionality, it just causes confusion.
    – smci
    Dec 5, 2016 at 20:42
  • 4
    @smci I do not agree with your premise that there is a need to adhere to standard semantics or even that there is such a standard. To my maths sensibilities it makes perfect sense that + got the lowest precedence.
    – Roland
    Dec 6, 2016 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .