布尔运算符 &&和 || [英] Boolean operators && and ||

查看:35
本文介绍了布尔运算符 &&和 ||的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 R 语言定义&&&(分别是|||)的区别在于前者是矢量化的,而后者不是.

According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former is vectorized while the latter is not.

根据帮助文本,我读到的区别类似于And"和AndAlso"(相应地Or"和OrElse")之间的区别......意义:并不是所有的评估都不是必须的(即,如果 A 为真,则 A 或 B 或 C 始终为真,因此如果 A 为真,则停止评估)

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)

有人可以在这里解释一下吗?另外,R 中有 AndAlso 和 OrElse 吗?

Could someone shed light here? Also, is there an AndAlso and OrElse in R?

推荐答案

较短的被向量化,意味着它们可以返回一个向量,像这样:

The shorter ones are vectorized, meaning they can return a vector, like this:

((-2:2) >= 0) & ((-2:2) <= 0)
# [1] FALSE FALSE  TRUE FALSE FALSE

较长的形式从左到右求值,只检查每个向量的第一个元素,所以上面给出了

The longer form evaluates left to right examining only the first element of each vector, so the above gives

((-2:2) >= 0) && ((-2:2) <= 0)
# [1] FALSE

正如帮助页面所说,这使得较长的形式适用于编程控制流,并且 [is] 通常首选在 if 子句中."

As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."

因此,只有当您确定向量的长度为 1 时,您才想使用长形式.

So you want to use the long forms only when you are certain the vectors are length one.

您应该绝对确定您的向量的长度仅为 1,例如在它们是仅返回长度为 1 的布尔值的函数的情况下.如果向量的长度可能 >1,则您想使用短形式.因此,如果您不确定,您应该先检查,或者使用简短形式,然后使用 allany 将其减少到长度为 1 以用于控制流语句,如 if.

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 and any to reduce it to length one for use in control flow statements, like if.

函数 allany 经常用于矢量化比较的结果,以分别查看所有或任何比较是否为真.这些函数的结果肯定是长度为 1,因此它们适合在 if 子句中使用,而矢量化比较的结果则不然.(虽然这些结果适用于 ifelse.

The functions all and any 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 in ifelse.

最后一个区别:&&|| 只评估他们需要的尽可能多的术语(这似乎是短路的意思).例如,这是一个使用未定义值 a 的比较;如果它没有短路,如 &| 没有短路,它会给出一个错误.

One final difference: the && and || only evaluate as many terms as they need to (which seems to be what is meant by short-circuiting). For example, here's a comparison using an undefined value a; if it didn't short-circuit, as & and | don't, it would give an error.

a
# Error: object 'a' not found
TRUE || a
# [1] TRUE
FALSE && a
# [1] FALSE
TRUE | a
# Error: object 'a' not found
FALSE & a
# Error: object 'a' not found

最后,请参阅 The R Inferno 中的第 8.2.17 节,标题为and and andand".

Finally, see section 8.2.17 in The R Inferno, titled "and and andand".

这篇关于布尔运算符 &amp;&amp;和 ||的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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