& 和有什么区别?和&&, |和 ||在R? [英] What's the differences between & and &&, | and || in R?

查看:81
本文介绍了& 和有什么区别?和&&, |和 ||在R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有四个逻辑运算符:

Why there are four logical operators:

&, &&
|, ||

使用上有什么不同?

是的,我已经检查了文档,但我有点困惑.文档说:

Yes, I've checked the docs, yet I'm a little bit confused. The docs says:

 ‘&’ and ‘&&’ indicate logical AND and ‘|’ and ‘||’ indicate
 logical OR.  The shorter form performs elementwise comparisons in
 much the same way as arithmetic operators.  The longer form
 evaluates left to right examining only the first element of each
 vector.  Evaluation proceeds only until the result is determined.
 The longer form is appropriate for programming control-flow and
 typically preferred in ‘if’ clauses.

我认为一个例子可以清楚地展示它们.谢谢.

I think a piece of example will clearly demonstrate them. Thanks.

推荐答案

主要区别如下...

  1. 长形式(&& 或 ||)短路,这意味着它是否可以通过仅验证第一个元素来识别结果.在执行 && 时,如果前两个元素的比较结果为 false,则比较下一组元素也会导致 False.所以,它返回false.在做 || 时如果比较结果在前几个元素中为真,我们可以自信地说任何进一步的验证都不会改变结果,因此它返回 True.

  1. Long form(&& or ||) short circuits, which means if it can identify the result by just validating just the first element. While doing &&, if the comparision of first two elements resulted in false, comparing next set of elements will also result in False. So, it returns false. While doing || if comparision resulted in true in first few elements, we can confidently say that any further validations will not change the result so it returns True.

短形式继续对整个向量执行并创建结果向量并返回它.

Short forms continues to do for the entire vectors and creates a vector of results and returns it.

希望这会有所帮助.

&和&&表示逻辑与和 |和 ||表示逻辑或.这较短的形式执行元素比较的方式与算术运算符.较长的形式从左到右求值只检查每个向量的第一个元素.评估收益直到结果确定.较长的形式是合适的用于编程控制流,通常首选在 if 子句中.

& and && indicate logical AND and | and || indicate logical OR. The shorter form performs elementwise comparisons in much the same way as arithmetic operators. The longer form evaluates left to right examining only the first element of each vector. Evaluation proceeds only until the result is determined. The longer form is appropriate for programming control-flow and typically preferred in if clauses.

来源:http://stat.ethz.ch/R-manual/R-patched/library/base/html/Logic.html

这篇关于& 和有什么区别?和&&, |和 ||在R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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