MAO-EYE 2011-07-02 18:48 采纳率: 100%
浏览 341
已采纳

布尔运算符 & & 和 | |

According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former is vectorized while the latter is not.

According to the help text, I read the difference akin to the difference between an "And" and "AndAlso" (correspondingly "Or" and "OrElse")... Meaning: That not all evaluations if they don't have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true)

Could someone shed light here? Also, is there an AndAlso and OrElse in R?

转载于:https://stackoverflow.com/questions/6558921/boolean-operators-and

  • 写回答

3条回答 默认 最新

  • 乱世@小熊 2011-07-02 19:13
    关注

    The shorter ones are vectorized, meaning they can return a vector, like this:

    ((-2:2) >= 0) & ((-2:2) <= 0)
    # [1] FALSE FALSE  TRUE FALSE FALSE
    

    The longer form evaluates left to right examining only the first element of each vector, so the above gives

    ((-2:2) >= 0) && ((-2:2) <= 0)
    # [1] FALSE
    

    As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."

    So you want to use the long forms only when you are certain the vectors are length one.

    You should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use all and any to reduce it to length one for use in control flow statements, like if.

    The functions all and any are often used on the result of a vectorized comparison to see if all or any of the comparisons are true, respectively. The results from these functions are sure to be length 1 so they are appropriate for use in if clauses, while the results from the vectorized comparison are not. (Though those results would be appropriate for use in ifelse.

    One final difference: the && and || only evaluate as many terms as they need to (which seems to be what is meant by short-circuiting). For example, here's a comparison using an undefined value a; if it didn't short-circuit, as & and | don't, it would give an error.

    a
    # Error: object 'a' not found
    TRUE || a
    # [1] TRUE
    FALSE && a
    # [1] FALSE
    TRUE | a
    # Error: object 'a' not found
    FALSE & a
    # Error: object 'a' not found
    

    Finally, see section 8.2.17 in The R Inferno, titled "and and andand".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False