link
And now something I once wrote on a Coursera thread regarding how I feel about the <-- operator always being taught as better than =.
Dan is right and this seems to be taught more often than necessary by people introducing students to R. It's very much a preference thing, but I am a staunch supporter of not recommending <- signs for assignments. (I'm not trying to convert anybody, but just to share my ranting opinion. I never hear anybody express this opinion, but I feel like it makes good sense, so here goes):
The problem I have is that we're always taught to use <- because, as Dan says, it "can lead to problems" to use =. Yet, I have been using R for about 5 years and the only case of ambiguity I'm aware of is when using a type of command which is way too advanced for beginning R users to care about in the first place. In fact, I used = sign as assignment for all 5 years and never once had a problem.
The advanced command I'm talking about is if you wanted to combine an assignment statement and a function call. In order to do so, the assignment statement needs to be executed with <-. The = sign won't work. Here's an example. You want to call a function with the argument being a uniform random number on (0,1). You could write
u = runif(1) # You could also write u <- runif(1) here, of course. That works.
myfunction(u)
This type of "one-at-a-time" statement execution is totally natural for new R users (ESPECIALLY non-programmers) and will never cause any problems by not using <- for the assignment. But, you could save a line and accomplish your goal by just typing
myfunction(u <- runif(1)) # Executes just fine, with no error.
myfunction(u = runif(1)) # Causes an error.
Just above, the part inside the parentheses of myfunction() evaluates first: a random uniform number on (0,1) is assigned to the variable u. Then, that value is passed to the function myfunction(). This doesn't work with an = sign.
Very few people who are new to R - if any - are going to care about doing things like this, so they'll use the first type of coding and be totally fine with using = as an assignment operator, pretty much forever. In year 4 or 5 of my R use I finally asked myself "can I assign a variable in the same line as a function call?" And when I found the answer in a forum, they were clear that <- had to be used rather than =. So I continue to use = for assignments to this day, with no problems.
<- is a pain in the butt to type every time I make assignments in R code! Let's make life easier for beginners and just tell them to use =.
Incidentally, I just found out that R Studio inserts <- for you if you just type Alt+_ (Alt plus underscore). If I had known this back in the day, maybe I'd have used <- all along... :)