rbooleanquery

In R programming, what is the difference between the any and | (or) Boolean operator?


As such aren't we saying the same thing here?

Are there any advantages of using one over the other?


Solution

  • | is vectorized--it returns a result with the same length as the longest input and will recycle if needed.

    any looks at all the inputs and returns result of length 1.

    || make only a single comparison, using the first elements of its inputs regardless of their length, and returns a result of length 1.

    x = c(FALSE, TRUE, FALSE)
    y = c(FALSE, FALSE, FALSE)
    
    any(x, y)
    # [1] TRUE
    ## There's a TRUE in there somewhere
    
    x | y
    # [1] FALSE  TRUE FALSE
    ## Only the 2nd index of the vectors contains a TRUE
    
    x || y
    # [1] FALSE
    ## The first position of the vectors does not contain a TRUE.
    

    If the inputs are all of length one, then x1 | x2 | x3 is equivalent to x1 || x2 || x3 is equivalent to any(x1, x2, x3). Otherwise, no guarantee.