Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there two assignment operators, `<-` and `->` in R?

I know how to use <- and ->, and there are several writeups on the difference between equals assignment & arrow assignment, but I don't know when to prefer -> over <-.

It seems the community has coalesced around using <- for assignment.

Neither the google R style-guide, nor Hadley Wickam's tidyverse R style-guide even mention -> in the assignment section.

I'm curious about the design considerations that led the S/S-PLUS developers to put in the right arrow assign operator ->. In what setting(s) would using -> be considered more readable (or easier to type) versus <- or =?

I'm not familiar with any other language that allows the right-assignment semantics. What languages inspired R in this regard?

I'm looking for answers that cite books / early design documents / user manuals / archived mailing lists or other references to establish what the S author/designer's intent was in putting in the forward-arrow assignment operator.

like image 427
Haleemur Ali Avatar asked Jul 26 '18 20:07

Haleemur Ali


People also ask

What is difference between <- and in R?

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

Should I use <- or in R?

The Google R style guide prohibits the use of “=” for assignment. Hadley Wickham's style guide recommends “<-” If you want your code to be compatible with S-plus you should use “<-”

What is the difference between := and assignment operators?

= operator assigns a value either as a part of the SET statement or as a part of the SET clause in an UPDATE statement, in any other case = operator is interpreted as a comparison operator. On the other hand, := operator assigns a value and it is never interpreted as a comparison operator.

Why does R use <-?

As you all know, R comes from S. But you might not know a lot about S (I don't). This language used <- as an assignment operator. It's partly because it was inspired by a language called APL, which also had this sign for assignment.


2 Answers

From the answer to an exercise in The New S Language (Becker, Chambers and Wilks 1988), via Google Books:

When you type a long expression only to remember at the end that it would be a good idea to save the result, a right-hand arrow allows you to perform an assignment without retyping the line.

This suggests that S users were working directly in the console, without line-editing capabilities that are available in most modern REPL/interactive environments ...


Some archaeology: I poked around in foundational sources on Google Books. There are three relevant books:

  • the Brown Book: S: An Interactive Environment for Data Analysis and Graphics R. A. Becker, J. M. Chambers (CRC Press, 1984)
  • Extending the S System, Becker and Chambers (Taylor & Francis, 1985)
  • the Blue Book: The New S Language Becker, Chambers and Wilks (Wadsworth and Brooks/Cole 1988, but reissued in 2018!! by CRC Press)

    1. The Brown Book doesn't mention -> in the main text:

enter image description here

but it does in the appendix:

enter image description here

  1. Extending the S System mentions -> in the main text:

enter image description here

I can't search very much of the book, so -> might also be mentioned in the appendix somewhere.

  1. The Blue Book refers to the right-arrow, but actually seems to have a typo:

enter image description here

I interpret the underlined red passages as supporting that there's a typo in the first underlined line, which should be -> rather than ← ...

Here's the screenshot of the exercise answer referred to above:

enter image description here

If you want a copy of the 1985 book you can get it for $34.41 - or $1070.99 (but with free shipping!) ...

enter image description here

like image 97
Ben Bolker Avatar answered Sep 20 '22 06:09

Ben Bolker


I think it is just a matter of personal preference.

Although -> predated magrittr pipes, one recent use case is that -> can be used to keep the flow from left to right in such pipes:

library(magrittr) input %>% fun1 %>% fun2 -> result 

On the other hand, given that <- is mostly used you might want to use <- even in that case.

The argument for <- is that it starts the line off with the value being set which is sort of like the purpose of the statement, particularly if the result variable is well named, whereas the right hand side is the mechanics and so is detail subservient to the result -- and one likes to start off with an overview and then only get into the detail later. Below we are defining parameter k. That it is 3 or whether it is defined by a constant as it is here or a complex expression seems incidental to the purpose of the statement.

k <- 3 

Personally, I never use ->.

like image 39
G. Grothendieck Avatar answered Sep 22 '22 06:09

G. Grothendieck