What is the difference between short (&,|) and long (&&, ||) forms of AND, OR logical operators in R? [duplicate]

不问归期 提交于 2019-11-27 04:38:24

问题


Possible Duplicate:
R: subset() logical-and operator for chaining conditions should be & not &&

What is the difference between short (&,|) and long (&&, ||) forms of AND, OR logical operators in R?

For example:

  1. x==0 & y==1
  2. x==0 && y==1
  3. x==0 | y==1
  4. x==0 || y==1

I always use the short forms in my code. Does it have any handicaps?


回答1:


& and | - are element-wise and can be used with vector operations, whereas, || and && always generate single TRUE or FALSE

theck the difference:

> x <- 1:5
> y <- 5:1
> (x > 2) & (y < 3) 
  [1] FALSE FALSE FALSE  TRUE  TRUE
> (x > 2) && (y < 3) # here operaand && takes only 1'st elements from logical
                     # vectors (x>2) and (y<3)
> FALSE

So, && and || are commonly used in if (condition) state_1 else state_2 statements, as dealing with vectors of length 1



来源:https://stackoverflow.com/questions/7953833/what-is-the-difference-between-short-and-long-forms-of-and-or-lo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!