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?
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 .
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.
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With