gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 11:58:35 27 4
gpt4 key购买 nike

这个问题在这里已经有了答案:




9年前关闭。




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



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

例如:
  • x==0 & y==1
  • x==0 && y==1
  • x==0 | y==1
  • x==0 || y==1

  • 我总是在我的代码中使用简短的形式。它有什么障碍吗?

    最佳答案

    &| - 是逐元素的,可以与向量运算一起使用,而,||&&始终生成单个 TRUEFALSE
    区别在于:

    > 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陈述,如
    处理长度向量 1

    关于r - R 中 AND、OR 逻辑运算符的短 (&,|) 和长 (&&, ||) 形式有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7953833/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com