What is the R assignment operator := for?

Romain Jacotin picture Romain Jacotin · Sep 28, 2015 · Viewed 7.9k times · Source

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?

Answer

James picture James · Sep 28, 2015

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