"="和"="之间有什么区别?和“<-" R中的赋值运算符? [英] What are the differences between "=" and "<-" assignment operators in R?

查看:181
本文介绍了"="和"="之间有什么区别?和“<-" R中的赋值运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

R中赋值运算符=<-之间有什么区别?

What are the differences between the assignment operators = and <- in R?

我知道运算符略有不同,如本例所示

I know that operators are slightly different, as this example shows

x <- y <- 5
x = y = 5
x = y <- 5
x <- y = 5
# Error in (x <- y) = 5 : could not find function "<-<-"

但这是唯一的区别吗?

But is this the only difference?

推荐答案

R中赋值运算符=<-之间有什么区别?

What are the differences between the assignment operators = and <- in R?

如您的示例所示,=<-的运算符优先级略有不同(确定它们混合在同一表达式中时的求值顺序).实际上,R中的 ?Syntax 给出以下内容运算符优先级表,从最高到最低:

As your example shows, = and <- have slightly different operator precedence (which determines the order of evaluation when they are mixed in the same expression). In fact, ?Syntax in R gives the following operator precedence table, from highest to lowest:

…
‘-> ->>’           rightwards assignment
‘<- <<-’           assignment (right to left)
‘=’                assignment (right to left)
…

但这是唯一的区别吗?

由于您正在询问赋值运算符:是的,那是唯一的区别.但是,您会相信其他理由.甚至 ?assignOps 的R文档都声称存在还有更多区别:

Since you were asking about the assignment operators: yes, that is the only difference. However, you would be forgiven for believing otherwise. Even the R documentation of ?assignOps claims that there are more differences:

运算符<-可以在任何地方使用, 而运算符=仅在最高级别(例如, 在命令提示符下键入的完整表达式中)或作为一个 大括号的表达式列表中的子表达式.

The operator <- can be used anywhere, whereas the operator = is only allowed at the top level (e.g., in the complete expression typed at the command prompt) or as one of the subexpressions in a braced list of expressions.

我们不要太在意: R文档是(巧妙地)错误 [

Let’s not put too fine a point on it: the R documentation is (subtly) wrong [1]. This is easy to show: we just need to find a counter-example of the = operator that isn’t (a) at the top level, nor (b) a subexpression in a braced list of expressions (i.e. {…; …}). — Without further ado:

x
# Error: object 'x' not found
sum((x = 1), 2)
# [1] 3
x
# [1] 1

很明显,我们已经在上下文(a)和(b)之外使用=进行了分配.那么,为什么几十年来错误地记录了核心R语言功能的文档呢?

Clearly we’ve performed an assignment, using =, outside of contexts (a) and (b). So, why has the documentation of a core R language feature been wrong for decades?

这是因为在R的语法中,符号=具有两种不同的含义,这些含义通常会被合并:

It’s because in R’s syntax the symbol = has two distinct meanings that get routinely conflated:

  1. 第一个含义是作为赋值运算符.到目前为止,我们已经讨论过这一切.
  2. 第二个含义不是运算符,而是一个语法标记,它表示函数调用中命名参数传递.与=运算符不同,它在运行时不执行任何操作,它仅更改表达式的解析方式.
  1. The first meaning is as an assignment operator. This is all we’ve talked about so far.
  2. The second meaning isn’t an operator but rather a syntax token that signals named argument passing in a function call. Unlike the = operator it performs no action at runtime, it merely changes the way an expression is parsed.

让我们看看.

以任何通用形式的代码...

In any piece of code of the general form …

‹function_name›(‹argname› = ‹value›, …)
‹function_name›(‹args›, ‹argname› = ‹value›, …)

... = 是定义命名参数传递的令牌:它不是赋值运算符.此外,在某些语法上下文中,=完全禁止:

if (‹var› = ‹value›) …
while (‹var› = ‹value›) …
for (‹var› = ‹value› in ‹value2›) …
for (‹var1› in ‹var2› = ‹value›) …

其中任何一个都会引发错误"‹bla›中出现意外的'='".

Any of these will raise an error "unexpected '=' in ‹bla›".

在任何其他上下文中,=均指代赋值运算符调用.特别是,仅在子表达式两边加上括号,即可使以上(a)以及(b)一个 assignment 都有效.例如,以下执行分配:

In any other context, = refers to the assignment operator call. In particular, merely putting parentheses around the subexpression makes any of the above (a) valid, and (b) an assignment. For instance, the following performs assignment:

median((x = 1 : 10))

而且:

if (! (nf = length(from))) return()

现在您可能会反对这样的代码是残酷的(您可能是正确的).但是我从base::file.copy函数中获取了这段代码(将<-替换为=)—在许多核心R代码库中,这是一种普遍的模式.

Now you might object that such code is atrocious (and you may be right). But I took this code from the base::file.copy function (replacing <- with =) — it’s a pervasive pattern in much of the core R codebase.

John Chambers的原始解释,R文档可能基于该解释,实际上可以正确解释这一点:

The original explanation by John Chambers, which the the R documentation is probably based on, actually explains this correctly:

[=赋值]仅在语法的两个位置允许:顶级(作为完整程序或用户键入的表达式);并且当通过括号或多余的一对括号与周围的逻辑结构隔离时.

[= assignment is] allowed in only two places in the grammar: at the top level (as a complete program or user-typed expression); and when isolated from surrounding logical structure, by braces or an extra pair of parentheses.


坦白:我早些说谎.在=<-运算符之间还有一个 区别:它们调用不同的函数.默认情况下,这些功能执行相同的操作,但是您可以分别覆盖其中的任何一个以更改行为.相比之下,<-->(从左到右的赋值)尽管在语法上是不同的,但始终调用 same 函数.覆盖一个也覆盖另一个.知道这几乎是不实际的但它可以用于某些有趣的恶作剧.


A confession: I lied earlier. There is one additional difference between the = and <- operators: they call distinct functions. By default these functions do the same thing but you can override either of them separately to change the behaviour. By contrast, <- and -> (left-to-right assignment), though syntactically distinct, always call the same function. Overriding one also overrides the other. Knowing this is rarely practical but it can be used for some fun shenanigans.

这篇关于"="和"="之间有什么区别?和“&lt;-" R中的赋值运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆