Skip to content
Home » The difference between

The difference between <- and = assignment in R

Tags:

When I started coding in R, a couple of years ago, I was stunned by the assignment operator. In most — if not all — coding languages I know, assignment is done through the equal sign ‘=’, while in R, I was taught to do it through the backarrow ‘<-‘. It didn’t take long until I realized the equal symbol also works. So… what is correct?

Let’s say we would like to assign the value 3 to the variable x. There’s multiple ways to do that.

assign(x,3) # Assign function
`<-`(x, 3) # Prefix/Polish notation of arrow shortcut
x <- 3 # Leftwards assignment
3 -> x # Rightwards assignment
x = 3
x <<- 3

The first five lines of code do exactly the same. The last one is slightly different. The assign function is the OG here: it assigns a value to a variable, and it even comes with more parameters that allow you to control which environment to save them in. By default, the variable gets stored in the environment it is being run in.

The arrows are respectively the leftwards assignment and the rightwards assignment. They simply are shortcuts for assign(). The double arrow in the last line is somewhat special. First, it checks if the variable already exists in the local environment, and if it doesn’t, it will store the variable in the global environment (.GlobalEnv). If you are unfamiliar with this, try reading up on scope in programming.

So why the arrow? Apparently, this is legacy from APL, a really old programming language. R (created in the early nineties) is actually a modern implementation of S (created in the mid-70’s), and S is heavily influenced by APL. APL was created on an Execuport, a now antique machine with a keyboard that had an “arrow” symbol, and it could be generated with one keystroke.

Historically, the = symbol was used to pass arguments to an expression. For example

rnorm(n = 10, mean = 2, sd = 1)

In 2001, to bring assignment in R more in line with other programming languages, assignment using the = symbol was implemented. There are restrictions, however. According to the documentation, it can only be used at the top-level environment, or when isolated from the surrounding logical structure (e.g. in a for loop).

But this is not necessarily true. By putting brackets around an assignment, in places where intuitively it shouldn’t work, one can get around these restrictions. There’s probably not many use cases where you would want it, but it does work. In the following example, I calculate the mean of 3, 20 and 30 and at the same time assign 3 to x.

mean(c((x = 3),20,30))

My personal advice is to use <- for assignment and = for passing arguments. When you mix things up, weird things can happen. This example from R Inferno produces two different results.

system.time(result = my.test.function(100))
system.time(result <- my.test.function(100))

By the way, if two keystrokes really bug you. There’s a shortcut in RStudio to generate the arrow: Alt + -. Well yes, shortcut? You still need to press two keys ;-).

By the way, if you’re having trouble understanding some of the code and concepts, I can highly recommend “An Introduction to Statistical Learning: with Applications in R”, which is the must-have data science bible. If you simply need an introduction into R, and less into the Data Science part, I can absolutely recommend this book by Richard Cotton. Hope it helps!

Great success!

Say thanks, ask questions or give feedback

Technologies get updated, syntax changes and honestly… I make mistakes too. If something is incorrect, incomplete or doesn’t work, let me know in the comments below and help thousands of visitors.

4 thoughts on “The difference between <- and = assignment in R”

  1. Tһank you, I’ve just been seaгching for info about tһіs topic for agеs
    and yours is the greatest I’ve found out so far. Hoԝevеr, what in regards to the bottom ⅼine?
    Arе you positive about the supply?

  2. An interesting discussion is worth comment. I believe that you should write more about this ѕubject matter, it
    might not Ƅe a taboo suƅject but usually foⅼks don’t sρeak about ѕuch subjects.

    To the next! Best wishеѕ!!

Leave a Reply

Your email address will not be published. Required fields are marked *