R中的AND,OR逻辑运算符的短(&,|)和长(&&,||)形式有什么区别?

时间:2011-10-31 12:40:59

标签: r logical-operators

  

可能重复:
  R: subset() logical-and operator for chaining conditions should be & not &&

简短的(&|)和长(&&||)形式的AND,OR逻辑运算符在R?

例如:

  1. x==0 & y==1
  2. x==0 && y==1
  3. x==0 | y==1
  4. x==0 || y==1
  5. 我总是在代码中使用简短形式。它有任何障碍吗?

1 个答案:

答案 0 :(得分:7)

&| - 是元素方面的,可以与向量操作一起使用,而||&&始终生成单个TRUE或{ {1}}

差点:

FALSE

因此,> 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 &&通常用于||语句中 处理长度为if (condition) state_1 else state_2

的向量
相关问题