Assignment Operators in R – Which One to Use and Where

Whenever you start learning a new programming language, you must get accustomed to the language’s syntax. One of the first operators you’d expect to come across is the assignment operator for the language. Assignment operators are used to, well, assign values to variables. The R language has a few different ways to assign values. Let’s take a quick look and them and some of their differences and use cases.

Assignment Operators

R has five common assignment operators:

  • <-
  • ->
  • <<-
  • ->>
  • =

Many style guides and traditionalists prefer the left arrow operator, <-. Why use that when it’s an extra keystroke? <- always means assignment. The equal sign is overloaded a bit taking on the roles of an assignment operator, function argument binding, or depending on the context, case statement.

Equal or “arrow” as an Assignment Operator?

In R, both the equal and arrow symbols work to assign values. Therefore, the following statements have the same effect of assigning a value on the right to the variable on the left:

x = 42

x <- 42

There is also a right arrow, -> which assigns the value on the left, to a variable on the right:

42 -> x

All three assign the value of forty-two to the variable x.

So what’s the difference? Are these assignment operators interchangeable? Mostly, yes. The difference comes into play, however, when working with functions.

The equal sign can also work as an operator for function parameters.

x <- 42
y <- 18
function(value = x-y)

History of the <- Operator

Where did the arrow as an assignment operator originate? As you may know, the R language has its origins in the S language. S was originally influenced, in part, by APL. APL had its own keyboards which included arrow symbols.

APL Keyboard Layout - Assignment Operators next to the "P" key

The S language also didn’t have == for equality testing, so that was left to the single equal sign. Therefore, variable assignment needed to be accomplished with a different symbol, and the arrow was chosen.


There are some differences of opinion as to which assignment operator to use when it comes to = vs <-. Some believe that = is more clear. The <- operator maintains backward compatibility with S. Google’s R Style Guide recommends using the <- assignment operator, which seems to be a pretty decent reason as well. When all is said and done, though, it is like many things in programming, it depends on what your team does.

R is a great language for data analysis. If you’re interested in learning how to use R to explore data in a MongoDB database, please check out this blog post I wrote. There are many uses for it, and knowing a bit about the assignment operators and which one to use should help tremendously.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.