A recent thread on the r-help mailing list raises a common question for beginning R users: should you use = (equals) or <- (back arrow) for assignments? In R, both of the following statements have the effect of assigning the value 3 to the variable x:
x = 3
x <- 3
So if they have the same effect, does it matter which you use?
A little history before we continue: when the R language (and S before it) was first created, <- was the only choice of assignment operator. This is a hangover from the language APL, where the arrow notation was used to distinguish assignment (assign the value 3 to x) from equality (is x equal to 3?). (Professor Ripley reminds me that on APL keyboards there was an actual key on the keyboard with the arrow symbol on it, so the arrow was a single keystroke back then. The same was true of the AT&T terminals first used for the predecessors of S as described in the Blue Book.) However many modern languages (such as C, for example) use = for assignment, so beginners using R often found the arrow notation cumbersome, and were prone to use = by mistake. But R uses = for yet another purpose: associating function arguments with values (as in pnorm(1, sd=2), to set the standard deviation to 2).
To make things easier for new users familiar with languages like C, R added the capability in 2001 to also allow = be used as an assignment operator, on the basis that the intent (assignment or association) is usually clear by context. So,
x = 3
clearly means "assign 3 to x", whereas
f(x = 3)
clearly means "call function f, setting the argument x to 3".
There is one case where ambiguity might occur: if you wanted to assign a variable during a function call. The only way to do this in modern versions of R is:
f(x <- 3)
which means "assign 3 to x, and call f with the first argument set to the value 3". This is a contrived example though, and never really occurs in real-world programming. [UPDATE: In fact, constructs like this are best avoided for reasons given in the comments below.]
So, back to the original question: should you use = or <- for assignment? It really boils down to preference. Many people are more used to using = for assignment, and it's one less keystroke if you want to save on typing. On the other hand, many R traditionalists prefer <- for clarity, and if you plan to share or publish your code, other might find code using = for assignment hard to read. Personally, I prefer the arrow notation, with a space on either side.
So, long story short: it's really up to you.
[Update Sep 2 2011: Added link to developer page for = assignment.]
(This article was first published on Why? » R, and kindly contributed to R-bloggers)
In R, you can use both ‘=’ and ‘<-’ as assignment operators. So what’s the difference between them and which one should you use?
What’s the difference?
The main difference between the two assignment operators is scope. It’s easiest to see the difference with an example:
Here is declared within the function’s scope of the function, so it doesn’t exist in the user workspace. Now, let’s run the same piece of code with using the <- operator:
This time the x variable is declared within the user workspace.
Which one should I use
Well there’s quite a strong following for the “<-” operator:
- 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 “<-”
- I believe that the General R community recommend using “<-”, but I can’t find anything on the mailing list.
However, I tend always use the “=” operator for the following reasons:
- It’s quicker to type “=” and “<-”.
- Typically, when I type declare a variable – I only want it to exist in the current workspace.
- Since I have the pleasure of teaching undergraduates their first course in programming, using “=” avoids misleading expressions like
Also Introducing Monte Carlo Methods with R, by Robert and Casella recommends using “=”.
If I’m missing something or you disagree, please leave a comment – I would be very interested.
To leave a comment for the author, please follow the link and comment on their blog: Why? » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...
If you got this far, why not subscribe for updatesfrom the site? Choose your flavor: e-mail, twitter, RSS, or facebook...