之间和功放的区别;和&功放;&安培; R中 [英] The difference between & and && in R

查看:96
本文介绍了之间和功放的区别;和&功放;&安培; R中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读

HTTP://stat.ethz。 CH / R-手动/ R-的devel /库/基/ HTML / Logic.html

和之间和放大器的区别;和&功放;&安培;没有意义。例如:

and the difference between & and && doesn't make sense. For example :

> c(1, 2, 3) & c(1,2,3)
[1] TRUE TRUE TRUE

根据这是预期行为的链接。它是做两个向量的各个元素的比较。

According to the link this is expected behavior. It is doing an element-wise comparison of the two vectors.

于是我再次测试...

So I test again...

> c(1, 2, 3) && c(1,2,3)
[1] TRUE

这也返回了什么预期。

但后来我改变值...

But then I change a value...

> c(1, 2, 3) && c(1,3,3)
[1] TRUE

仍有望因为第一个元素就可以了短路。

Still expected because it short circuits on the first element.

> c(1, 2, 3) & c(1,3,3)
[1] TRUE TRUE TRUE

这不过失去了我。这两个向量并不应相等。

This however lost me. These two vectors should not be equal.

推荐答案

&安培; 是一个逻辑运算符中以r比较之前覆羽您的批量逻辑值。对于数值的任何非0(和非NA /空/ NaN的东西)获取值为TRUE和0得到FALSE。因此,与所述事情让相当多的感觉

& is a logical operator so R coverts your quantities to logical values before comparison. For numeric values any non-0 (and non-NA/Null/NaN stuff) gets the value TRUE and 0 gets FALSE. So with that said things make quite a bit of sense

> as.logical(c(1,2,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,3,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,2,3)) & as.logical(c(1,2,3))
[1] TRUE TRUE TRUE
> as.logical(c(1,2,3)) & as.logical(c(1,3,3))
[1] TRUE TRUE TRUE

这篇关于之间和功放的区别;和&功放;&安培; R中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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