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

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

问题描述

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

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

有人可以照亮这里吗? 另外,R中是否有AndAlso和OrElse?

解决方案

较短的是矢量化的,这意味着它们可以返回矢量,如下所示:

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

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

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

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

因此,仅在确定向量的长度为一时,才想使用长格式.

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

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

最后一个区别:&&||仅计算所需数量的项(这似乎是短路).例如,这是使用未定义值a的比较;如果它没有短路,例如&|没有短路,则会出现错误.

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

最后,请参见 R地狱中的8.2.17节,标题为"and and andand".

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?

解决方案

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

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.

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.

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.

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

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

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

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