Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the R assignment operator := for?

Tags:

By digging into R source code (file R-3.2.2/src/main/gram.y lines 2836 to 2852) I found that the R parser/tokenizer considers that := is a LEFT_ASSIGNMENT token.

But when trying to use it as an assignment operator in R.3.2.2,
I have an error (impossible to find function for := ...) but as you can see R considers it as an assignment like <- :

> myVar := 42 Erreur : impossible de trouver la fonction ":=" > := Erreur : unexpected assignment in ":=" > <- Erreur : unexpected assignment in "<-" 

Is it a bug, or does the token := need to be removed from the tokenizer source code?

Is there a past story about := operator in R?

like image 862
Romain Jacotin Avatar asked Sep 28 '15 07:09

Romain Jacotin


People also ask

What is an assignment operator in R?

The first operator you'll run into is the assignment operator. The assignment operator is used to assign a value. For instance we can assign the value 3 to the variable x using the <- assignment operator. We can then evaluate the variable by simply typing x at the command line which will return the value of x .

Why does R use assignment operator?

As you all know, R comes from S. But you might not know a lot about S (I don't). This language used <- as an assignment operator. It's partly because it was inspired by a language called APL, which also had this sign for assignment.

Can you use equals in R?

even though the equal sign is also allowed in R to do exactly the same thing when we assign a value to a variable. However, you might feel inconvenient because you need to type two characters to represent one symbol, which is different from many other programming languages. as the assignment operator?


1 Answers

It was a previously allowed assignment operator, see this article from John Chambers in 2001.

The development version of R now allows some assignments to be written C- or Java-style, using the = operator. This increases compatibility with S-Plus (as well as with C, Java, and many other languages).

All the previously allowed assignment operators (<-, :=, _, and <<-) remain fully in effect.

It seems the := function is no longer present, but you can "re-enable it" like this:

`:=` <- `<-` x:=3 x [1] 3 
like image 141
James Avatar answered Sep 18 '22 13:09

James