R DISCUSSION

Arrow or equals sign for assignment in R: is there any real difference between the two?

Started by sherhasankhan R assignment operatorarrow vs equalsnamed argumentsoperator precedenceR style
4 replies 248 views 5 participants
Latest activity · 30 Sep 2026

Arrow or equals sign for assignment in R: is there any real difference between the two?

sherhasankhan R Forum
#1

Coming from C and Python I write x = 5 in R and it works, but every style guide and most examples use x <- 5. A colleague says they are identical, yet system.time(result = fit(data)) gave me an "unused argument" error while the arrow version ran fine.

Where exactly do the two operators behave differently, and is the arrow only a convention or is there a technical reason to prefer it?

Community replies 4

Re: Arrow or equals sign for assignment in R: is there any real difference between the two?

#2

At the top level of a script or inside a function body, x = 5 and x <- 5 do the same thing. The difference is inside a function call's argument list. There, = is not assignment at all; it names an argument. system.time(result = fit(data)) says "pass fit(data) as the argument named result", and system.time has no such argument.

system.time(result <- fit(data)) passes one unnamed expression that, when evaluated, assigns in your workspace, so afterwards result exists.

Re: Arrow or equals sign for assignment in R: is there any real difference between the two?

#3

The mirror-image mistake is using the arrow where you meant to name an argument. plot(x, y, col <- "red") creates a variable col in your environment and passes "red" as the third positional argument, which is not the colour. data.frame(a <- 1:3) similarly gives an oddly named column and leaves a stray a behind.

So the rule is simple: = for naming arguments, and one consistent choice for assignment. Using <- for assignment keeps the two meanings visibly separate.

Re: Arrow or equals sign for assignment in R: is there any real difference between the two?

#4

They also have different precedence: <- binds more tightly than =. a = b <- 5 works and sets both, but a <- b = 5 is an error, because it is parsed as (a <- b) = 5. It rarely matters, but it is a real difference in the grammar.

One spacing trap belongs to the arrow: x<-3 assigns 3 to x, whereas x < -3 compares. If you meant the comparison and left out the spaces, you have just overwritten your variable. Writing spaces around operators avoids it.

Re: Arrow or equals sign for assignment in R: is there any real difference between the two?

#5

For completeness, there are related operators. <<- does not create a local variable; it looks for an existing variable in the enclosing environments and modifies that one, falling back to the global environment. It is useful in closures that keep state, such as a counter, and a source of hidden coupling almost everywhere else. -> assigns to the right and is sometimes used at the end of a long pipeline.

R also rejects if (x = 5) as a syntax error, so the classic C slip of assigning inside a condition cannot happen by accident.

TEP COMMUNITY