What's the reasoning behind using <- as assignment operator and not =?

Why do you prefer using <- over =?

All modern languages use = for assignment. S is long gone and _ is no assignment operator in R either.

with a right hand arrow, you may use -> but it is discouraged because it's more difficult to spot.

being able to use <<- is handy but difficult to spot, in my opinion.

If it is preferable to =, shouldn't other languages switch as well?

Something it inherited from APL apparenly , which used non-ascii characters, so programmers used keyboards like APL (programming language) - Wikipedia

No language besides R uses it today. Other languages even make use of the arrow in different ways. R limits itself to assignment which is already covered by the equal sign.

If you read ?assignOps, you'll find that there are three assignment operators in R and two of them have left and right forms, so five in total.

There, also, is the advantage that <- clearly shows it is an assignment whereas ** xx = yy** may be a logical test.

dat1  <-  structure(list(aa = c(1, 2, 1, 2), bb = 12:15, cc = 1:4), class = "data.frame",
                    row.names = c(NA, -4L))

ggplot(dat1, aes(cc, bb, colour = as_factor(aa))) + geom_point()
1 Like

First, a detail, there are still a few modern languages that use e.g. := for assignment (Maple and TLA+ come to mind). But indeed most languages converged towards =. Still some language might require a let keyword, are they wrong?

In practice, it's a bit of a detail, doesn't change much in practice. There is no way the R core would change this, since it would break pretty much all existing code. So in the end it's a matter of programmer and style guide preferences. There are people who write R code with =.

I can give my personal opinion: since = is used for assignment-like operations with a slightly different meaning (e.g. in function arguments), I prefer to use <- to clearly distinguish them. In the code

my_global <- f(arg = 1)

it's very clear and obvious that my_global is a global variable, and I'm changing the state of the program, whereas arg is more local and doesn't affect the global state.

I find it useful when writing big code blocks with lots of pipes: you can just look for the <- to see what the goal of the code block is, whereas all the function calls with their = inside the block are the details of how it's computed.

Of course, other languages manage fine with only =, each language has its own ways to try and make code clear.