Assignment

R has many ways to perform assignment. Some examples of assigning the name x to the object 42 include:

Assignment to the right-hand side via the -> operator is generally discouraged by the community. While I agree in some situations, I think there is reason to consider -> at the end of a sequence of piped expressions, as the flow of the output is in some sense “to the right” when using the |> or %>% pipe operators.1

The assign() function is useful in that it allows the user to specify the environment in which the assignment should take place. However, it is not often used as it requires much more typing than an operator, and many users do not need to manipulative the environment where assignment takes place.

While both -> and assign() are interesting and potentially useful, they are not the focus of this document.

Instead, this document will focus on the use of and differences between <- and =, the two most commonly used assignment operators in R. Most R style guides will suggest the use of <- while explicitly discouraging the use of =, often with no explanation of why.

“Use <-, not =, for assignment.”

The tidyverse style guide

I strongly prefer the use of = over <- for assignment. To be clear, this is a personal preference. I am not claiming that this is “correct” or that you should do the same. But I think it is useful to fully understand the history and differences of these two operators.

Ease of Entry

  • TODO: = is one simple keystroke
  • TODO: <- is at least three
  • TODO: yes there is an RStudio shortcut for <-, but again, more than 1 keystroke

Cross-Language Consistency

  • TODO: most folks coming from other languages use =, in particular Python folks, our data friends

Style

  • TODO: personally, i like the way = looks and really do not like the way <- looks at all
  • TODO: this is 100% subjective
  • TODO: if you’re going to use <- be use to put spaces around it to avoid the ambiguity that a reader my have when reading something like x<-1 is that x <- 1 or x < -1???

What’s the Difference

  • TODO: Suppose we want to time how long R take to run the following code:
x = 10 * 7 - 5 # this is a silly thing to time, but oh well
# this creates a problem
system.time(x = 10 * 7 - 5)
Error in system.time(x = 10 * 7 - 5) : unused argument (x = 10 * 7 - 5)
  • TODO: explain what is happening here, R thinks x is an argument to the call here, system.time(), but x is not an argument to system.time(), we need to get R to understand we’re trying to make an assignment
# this would work as expected
system.time(x <- 10 * 7 - 5)
   user  system elapsed 
      0       0       0 
  • TODO: in general, the solution is to brace the expression with {}, which is frankly clearer because things like mean(x <- 100) are allowed
# this solves the initial problem
system.time({x = 10 * 7 - 5})
   user  system elapsed 
      0       0       0 
# if you're going to use `<-`, i think this would be clearer
system.time({x <- 10 * 7 - 5})
   user  system elapsed 
      0       0       0 
  • TODO: note better ways to time r code like rbenchmark and microbenchmark

TODO

  • TODO: link to coatless comments
  • TODO: ?assign
  • TODO: ?=
  • TODO: it was made to be part of the language, so why not use it? (find changelog where it was added?)
  • TODO: history form APL and keyboards that supported <- much more easily
  • TODO: ligatures and why I don’t want to use them (students are very perceptive to visual differences, installing fonts is annoying, less setup is good, “default is beautiful”)
  • TODO: other = users, there are dozens of us
  • TODO: restate PEP 8 quote
  • TODO: this feels dangerous: mean(x <- 1:100)
  • TODO: https://github.com/Robinlovelace/geocompr/issues/319
  • TODO: https://github.com/csgillespie/efficientR/issues/34
  • TODO: https://www.youtube.com/watch?v=88TftllIjaY&t=2101s
  • TODO: kind of wish this would work x -> 10
  • TODO: should we just always do assign("var", obj)?
  • TODO: https://www.stat.auckland.ac.nz/%7Eihaka/downloads/JSM-2010.pdf
  • TODO: = doesn’t seem to work when creating “replacement” functions
  • TODO: easier to find assignments via <-
  • TODO: google style guide: https://google.github.io/styleguide/Rguide.html
  • TODO: https://jef.works/R-style-guide/
  • TODO: what does the styler package do by default? other linters? lintr
  • TODO: http://adv-r.had.co.nz/Style.html
  • TODO: note odd relationship between google and tidy style guides
  • TODO: why do I use = for assignment instead of <-?
  • TODO: evidence that <- is more popular, link to tidy style guide
  • TODO: move this “opinion piece” outside of the book, add a link to it in the discussion of assignment
  • TODO: reference the “names” chapter of advanced R
  • TODO: split into pros and cons? arguments for and arguments against? only “for” then transparently note downsides?
  • TODO: 1.8 “The assignment operator _' has been removed." https://cran.r-project.org/doc/manuals/NEWS.1 - also: 1.4 "The operator "=" is now allowed as an assignment operator in the grammar, for consistency with other languages, including recent versions of S-PLUS. Assignments with "=" are basically allowed only at top-level and in braced or parenthesized expressions, to make famous errors such as "if(x=0) 1 else 2" illegal in the grammar. (There is a plan to gradually eliminate the underscore as an assignment in future versions of R.)" - oh no, does this mean=` exits”because S”? https://twitter.com/whydoesr
  • TODO: within() is an issue, need braces to work
  • TODO: https://github.com/mlr-org/mlr3/wiki/Style-Guide
  • TODO: https://blog.r-hub.io/2022/03/21/code-style/

Footnotes

  1. TODO: The Google R style guide notes that if -> is used for assignment, unassumingly in addition to <-, that finding assignment is then more difficult as there are two operators to search for.↩︎