Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

11
  • 15
    This is also mentioned in chapter 8.2.26 of The R Inferno by Patrick Burns (Not me but a recommendation anyway) Commented Jun 14, 2016 at 9:17
  • 6
    I just realised that your explanation of how x <- x = 5 gets interpreted is slightly wrong: In reality, R interprets it as ​`<-<-`(x, y = 5, value = 5) (which itself is more or less equivalent to tmp <- x; x <- `<-<-`(tmp, y = 5, value = 5)). Yikes! Commented Jul 27, 2018 at 11:25
  • 15
    … And I just realised that the very first part of this answer is incorrect and, unfortunately, quite misleading because it perpetuates a common misconception: The way you use = in a function call does not perform assignment, and isn’t an assignment operator. It’s an entirely distinct parsed R expression, which just happens to use the same character. Further, the code you show does not “declare” x in the scope of the function. The function declaration performs said declaration. The function call doesn’t (it gets a bit more complicated with named ... arguments). Commented Apr 12, 2019 at 10:33
  • 1
    @ClarkThomborson The semantics are fundamentally different because in R assignment is a regular operation which is performed via a function call to an assignment function. However, this is not the case for = in an argument list. In an argument list, = is an arbitrary separator token which is no longer present after parsing. After parsing f(x = 1), R sees (essentially) call("f", 1). Whereas for x = 1 R sees call("=", "x", 1). It's true that in both cases name binding also happens but, for the assignment operator, it happens after calling the assignment operator function. Commented Jan 12, 2023 at 15:35
  • 2
    @JAQuent, the intention wasn't that the expression median(x = 1:10) errors. It is to contrast it to side-effect of median(x <- 1:10), which creates an assignment in the parent frame. Perhaps a bit subtle, but after the median(...) statements there is an x and that is what generates the error. After running median(x = 1:10) there is no x in the global environment (in this case), so evaluating x errors. Commented Jun 28, 2024 at 20:56