1
  • (cond1 | cond2 | cond3 | ...) means "is one or more of a bunch of conditions true?"
  • any(cond1, cond2, cond3 ....) means "are any of the conditions true?"

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

Are there any advantages of using one over the other?

zx8754
  • 52,746
  • 12
  • 114
  • 209
computronium
  • 445
  • 2
  • 11

1 Answers1

7

| 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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294