简体   繁体   English

什么是R赋值运算符:= for?

[英]What is the R assignment operator := for?

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. 通过挖掘R源代码(文件R-3.2.2/src/main/gram.y2836行到第2852)我发现R解析器/标记器认为:=LEFT_ASSIGNMENT标记。

But when trying to use it as an assignment operator in R.3.2.2 , 但是当试图在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 <- : 我有一个错误(无法找到函数:= ...)但是你可以看到R认为它是一个类似<-的赋值:

> 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? 它是一个bug,还是令牌:=需要从tokenizer源代码中删除?

Is there a past story about := operator in R? 是否有一个过去的故事:= R中的运算符?

It was a previously allowed assignment operator, see this article from John Chambers in 2001. 这是一个以前允许的任务操作员,请参阅2001年John Chambers的这篇文章

The development version of R now allows some assignments to be written C- or Java-style, using the = operator. 现在,R的开发版本允许使用=运算符将某些赋值写为C-或Java-样式。 This increases compatibility with S-Plus (as well as with C, Java, and many other languages). 这增加了与S-Plus(以及C,Java和许多其他语言)的兼容性。

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 = . 为了澄清, R赋值运算符是<-=

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. 关于:= ,此运算符是data.table包中的j运算符。 It can be read defined as and is only usable in a data.table object. 它可以被defined as只读defined as并且只能在data.table对象中使用。 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 : 为了说明这一点,当第一个col中的值等于1时,我们将第二列修改为b (使用值b定义col2 ):

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 . 虽然您不能直接使用:= as =<- ,但是:=运算符在使用非标准评估(NSE)的域特定语言(DSL)编程时非常有用,例如dplyrdata.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) 在上面的例子中,在my_mutate函数中用=替换:= =将不起作用,因为!! mean_name = mean(!! expr) !! mean_name = mean(!! expr) isn't valid R code. !! mean_name = mean(!! expr)无效R代码。

You can read more about NSE and programming with dplyr here . 您可以在此处阅读有关NSE和使用dplyr编程的更多信息。 It does a great job explaining how to handle NSE when using dplyr functions to write your own function. 在使用dplyr函数编写自己的函数时,它可以很好地解释如何处理NSE。 My example above is directly copied from the website. 我上面的例子直接从网站上复制过来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM