What is the R assignment operator := for?

梦想与她 提交于 2019-11-29 09:11:46

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) 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!