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?
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
To clarify, the R
assignment operators are <-
and =
.
To get information about them type:
?`<-`
Instead of <-
in your command line. There also exists an operator <<-
affecting the variable in the parent environment.
Regarding :=
, this operator is the j
operator in data.table
package. It can be read defined as
and is only usable in a data.table
object. To illustrate this we modify the second column to b
(define col2
with value b
) when values in the first col are equal to 1
:
library(data.table)
dt = data.table(col1=c(1,2,1,2,3), col2 = letters[1:5])
dt[col1==1,col2:='b']
For detail explanation:
?`:=`
Hope it clarifies.
(Note: This is not a direct answer to the original question. Since I don't have enough reputation to comment and I think the piece of information below is useful, I put it as an answer anyway. Please let me know if there is a better way to do so!)
Although you can't directly use :=
as =
or <-
, the :=
operator is very useful in programming with domain specific language (DSL) that use nonstandard evaluation (NSE), such as dplyr
and data.table
. Below is an example:
library(dplyr)
df <- tibble(
g1 = c(1, 1, 2, 2, 2),
g2 = c(1, 2, 1, 2, 1),
a = sample(5),
b = sample(5)
)
my_mutate <- function(df, expr) {
expr <- enquo(expr)
mean_name <- paste0("mean_", quo_name(expr))
sum_name <- paste0("sum_", quo_name(expr))
mutate(df,
!! mean_name := mean(!! expr),
!! sum_name := sum(!! expr)
)
}
my_mutate(df, a)
#> # A tibble: 5 x 6
#> g1 g2 a b mean_a sum_a
#> <dbl> <dbl> <int> <int> <dbl> <int>
#> 1 1. 1. 1 3 3. 15
#> 2 1. 2. 4 2 3. 15
#> 3 2. 1. 2 1 3. 15
#> 4 2. 2. 5 4 3. 15
#> # ... with 1 more row
In the example above, replacing :=
within the my_mutate
function with =
won't work, because !! mean_name = mean(!! expr)
!! mean_name = mean(!! expr)
isn't valid R code.
You can read more about NSE and programming with dplyr
here . It does a great job explaining how to handle NSE when using dplyr
functions to write your own function. My example above is directly copied from the website.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.