布尔运算符 && 之间的区别和&和 || 之间和 |在R中
根据R语言定义, &
和 &&
之间的区别(对应 |
和 ||
)是前者是矢量化,而后者则不是。
根据帮助文本,我阅读类似于“And”和“AndAlso”之间的差异(相应地“Or”和“OrElse”)... 意义: 并非所有评估,如果不需要的话(即,如果 A 为真,则 A 或 B 或 C 总是为真,所以停止评估 A 是否为真)
有人可以在这里阐明吗? 另外,R 中有 AndAlso 和 OrElse 吗?
According to the R language definition, the difference between &
and &&
(correspondingly |
and ||
) is that the former is vectorized while the latter is not.
According to the help text, I read the difference akin to the difference between an "And" and "AndAlso" (correspondingly "Or" and "OrElse")...
Meaning:
That not all evaluations if they don't have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true)
Could someone shed light here?
Also, is there an AndAlso and OrElse in R?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
较短的形式是向量化的,这意味着它们可以返回一个向量,如下所示:
较长的形式则不然,因此(从 4.3.0 开始)必须给出长度为 1 的输入。(万岁!所需的检查较少,请参见下文。)
直到 R 4.3.0,给出 &&输入长度> 1 没有抛出错误,而是从左到右评估,仅检查每个向量的第一个元素,因此上面给出:
正如帮助页面所述,这使得较长的形式“适合对控制流进行编程,并且通常是优先用于 if 子句。”
因此,只有当您确定向量的长度为一时,您才需要使用长形式,并且从 4.3.0 开始,R 强制执行此操作。
如果您使用的是以前的版本,您应该绝对确定您的向量的长度仅为 1,例如在它们是仅返回长度 1 布尔值的函数的情况下。如果向量的长度可能大于 1,则需要使用缩写形式。因此,如果您不确定,您应该首先检查,或者使用缩写形式,然后使用
all
和any
将其缩减为长度,以便在控制中使用流语句,如if
。函数
all
和any
通常用于向量化比较的结果,以分别查看所有或任何比较是否为 true。这些函数的结果肯定是长度为 1,因此它们适合在 if 子句中使用,而矢量化比较的结果则不然。 (尽管这些结果适合在ifelse
中使用。)最后一个区别:
&&
和||
仅评估尽可能多的结果根据需要使用术语(通常称为短路)。例如,这里是使用未定义值a
的比较;如果它没有短路(如&
和|
那样),则会给出错误。最后,请参阅 The R Inferno 中的第 8.2.17 节,标题为“和和安和”。
The shorter ones are vectorized, meaning they can return a vector, like this:
The longer form is not, and so (as of 4.3.0) must be given inputs of length 1. (Hooray! Less checking necessary, see below.)
Until R 4.3.0, giving && inputs of length > 1 did not throw an error, but instead evaluated left to right examining only the first element of each vector, so the above gave:
As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."
So you want to use the long forms only when you are certain the vectors are length one, and as of 4.3.0, R enforces this.
If you're using a previous version, you should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use
all
andany
to reduce it to length one for use in control flow statements, likeif
.The functions
all
andany
are often used on the result of a vectorized comparison to see if all or any of the comparisons are true, respectively. The results from these functions are sure to be length 1 so they are appropriate for use in if clauses, while the results from the vectorized comparison are not. (Though those results would be appropriate for use inifelse
.)One final difference: the
&&
and||
only evaluate as many terms as they need to (which is often called short-circuiting). For example, here's a comparison using an undefined valuea
; if it didn't short-circuit, as&
and|
don't, it would give an error.Finally, see section 8.2.17 in The R Inferno, titled "and and andand".
关于“短路”的答案可能会产生误导,但有一定道理(请参阅以下)。在 R/S 语言中,
&&
和||
仅计算第一个参数中的第一个元素。无论第一个元素的值如何,向量或列表中的所有其他元素都将被忽略。这些运算符旨在与if (cond) {} else{}
构造配合使用,并指导程序控制而不是构造新向量。&
和 < code>| 运算符被设计用于处理向量,因此它们将“并行”应用,可以说,沿着最长参数的长度。在进行比较之前需要评估两个向量。如果向量的长度不同,则执行较短参数的回收。当计算
&&
或||
的参数时,如果从左到右连续的任何值是决定性的,则存在“短路” ,然后停止评估并返回最终值。只有当参数需要很长时间才能评估时,短路的优点才会显现出来。当参数是处理较大对象或具有更复杂数学运算的函数时,通常会发生这种情况。
更新:最新版本的
news(“R”)
表示,向&&
或||
已弃用并发出警告,RCore 的目的是使其在 R 的后续版本中成为错误。The answer about "short-circuiting" is potentially misleading, but has some truth (see below). In the R/S language,
&&
and||
only evaluate the first element in the first argument. All other elements in a vector or list are ignored regardless of the first ones value. Those operators are designed to work with theif (cond) {} else{}
construction and to direct program control rather than construct new vectors.. The&
and the|
operators are designed to work on vectors, so they will be applied "in parallel", so to speak, along the length of the longest argument. Both vectors need to be evaluated before the comparisons are made. If the vectors are not the same length, then recycling of the shorter argument is performed.When the arguments to
&&
or||
are evaluated, there is "short-circuiting" in that if any of the values in succession from left to right are determinative, then evaluations cease and the final value is returned.The advantage of short-circuiting will only appear when the arguments take a long time to evaluate. That will typically occur when the arguments are functions that either process larger objects or have mathematical operations that are more complex.
Update: The most recent edition of
news(“R”)
says that supplying vectors of length greater than 1 to&&
or||
is deprecated with a warning and the intent of RCore is to make it an error in a subsequent version of R.&&
和||
就是所谓的“短路”。这意味着如果第一个操作数足以确定表达式的值,则它们不会计算第二个操作数。例如,如果
&&
的第一个操作数为 false,则计算第二个操作数就没有意义,因为它无法更改表达式的值 (false && ; true
和false && false
都是 false)。当第一个操作数为 true 时,||
也是如此。您可以在此处阅读有关此内容的更多信息:http://en.wikipedia.org/wiki/Short-Circuit_evaluation在该页面上的表格中,您可以看到
&&
相当于 VB.NET 中的AndAlso
,我假设您指的是它。&&
and||
are what is called "short circuiting". That means that they will not evaluate the second operand if the first operand is enough to determine the value of the expression.For example if the first operand to
&&
is false then there is no point in evaluating the second operand, since it can't change the value of the expression (false && true
andfalse && false
are both false). The same goes for||
when the first operand is true.You can read more about this here: http://en.wikipedia.org/wiki/Short-circuit_evaluation From the table on that page you can see that
&&
is equivalent toAndAlso
in VB.NET, which I assume you are referring to.运算符
&&
/||
和&
/之间存在三个相关差异 |
,在 官方文档。总结如下:1.
&
和|
是向量化这意味着,如果您想对向量执行逐元素逻辑运算,您应该使用
&
和|
:在以前版本的 R 中,
a || b
(和a && b
)没有导致错误。相反,这些操作会默默地截断输出(仅返回第一个元素;在导致此错误之前,它会在 R 4.2 中短暂引发警告)。2.
&&
和||
是短路的短路意味着只计算表达式的右侧如果左侧尚未确定结果。几乎每种编程语言都对条件操作执行此操作,因为在编写
if
条件时会产生方便的习惯用法,例如:此代码依赖于短路:没有它,代码将失败 if
x
为空,因为右侧尝试访问不存在的元素。如果没有短路,我们将不得不使用嵌套的if
块,从而导致更冗长的代码:作为一般规则,在条件表达式内 (
if
,while
)你应该总是使用&&
和||
,即使不需要短路:它更重要惯用的,并导致更统一的代码。3.
&
和|
可以执行按位算术在许多(大多数?)编程语言中,
&
和|
实际上执行的是按位算术而不是布尔算术。也就是说,对于两个整数a
和b
,a & b
计算按位与,以及a | b
计算按位或。对于布尔值,按位运算和逻辑运算之间没有区别;但对于任意整数,结果不同。例如,1 |在大多数编程语言中,2 == 3
。然而,对于 R 来说,情况并非如此:R 将
&
和|
的数字参数强制转换为逻辑值并执行布尔算术。…除非两个参数都是
raw
类型:值得注意的是,操作
!
(逻辑非)和xor
也会在以下情况下执行按位算术运算:使用raw
参数调用。There are three relevant differences between the operators
&&
/||
and&
/|
, which are explained in the official documentation. Here’s a summary:1.
&
and|
are vectorisedThis means that if you want to perform element-wise logical operations on vectors you should use
&
and|
:In previous versions of R,
a || b
(anda && b
) did not cause an error. Instead, the operations silently truncated the output (only the first element was returned; before making this an error it briefly caused a warning in R 4.2).2.
&&
and||
are short-circuitedShort-circuiting means that the right-hand side of the expression is only evaluated if the left-hand side does not already determine the outcome. Pretty much every programming language does this for conditional operations, since it leads to handy idioms when writing
if
conditions, e.g.:This code relies on short-circuiting: without it, the code would fail if
x
is empty, since the right-hand side attempts to access a non-existent element. Without short-circuiting, we would have to use nestedif
blocks, leading to more verbose code:As a general rule, inside a conditional expression (
if
,while
) you should always use&&
and||
, even if short-circuiting isn’t required: it’s more idiomatic, and leads to more uniform code.3.
&
and|
can perform bitwise arithmeticIn many (most?) programming languages,
&
and|
actually perform bitwise arithmetic instead of Boolean arithmetic. That is, for two integersa
andb
,a & b
calculates the bitwise and, anda | b
calculates the bitwise or. For Boolean values there’s no difference between bitwise and logical operations; but for arbitrary integers, the result differs. For instance,1 | 2 == 3
in most programming languages.However, this is not true for R: R coerces numeric arguments of
&
and|
to logical values and performs Boolean arithmetic.… except when both arguments are of type
raw
:It is worth noting that the operations
!
(logical negation) andxor
also perform bitwise arithmetic when called withraw
arguments.