在 R 编程中,any 和 | 之间有什么区别?(或)布尔运算符?

use*_*754 1 r booleanquery

  • (cond1 | cond2 | cond3 | ...)意思是“一组条件中的一个或多个是否为真?”
  • any(cond1, cond2, cond3 ....)意思是“任何条件都为真?”

因此,我们在这里不是说同样的话吗?

使用一种比另一种有什么优势吗?

Gre*_*gor 7

| 被向量化——它返回一个与最长输入长度相同的结果,并在需要时回收。

any 查看所有输入并返回长度为 1 的结果。

|| 只进行一次比较,使用其输入的第一个元素,而不考虑它们的长度,并返回长度为 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.
Run Code Online (Sandbox Code Playgroud)

如果输入的长度均为 1,则x1 | x2 | x3等于x1 || x2 || x3等于any(x1, x2, x3)。否则,无法保证。