gpt4 book ai didi

r - 为什么当没有真值时 any() 返回 NA

转载 作者:行者123 更新时间:2023-12-04 10:33:09 25 4
gpt4 key购买 nike

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





Logical operators (AND, OR) with NA, TRUE and FALSE

(2 个回答)


4年前关闭。




所以我们有这种行为:

any(c(TRUE, FALSE, NA))
#> [1] TRUE
any(c(TRUE, NA))
#> [1] TRUE
any(c(FALSE, NA))
#> [1] NA

有人知道返回 NA 而不是 FALSE 的理由吗? IMO 该功能应该测试是否存在非 FALSE 值,而 NA 不是。

最佳答案

帮助文件的值部分解释了此行为:

The value returned is TRUE if at least one of the values in x is TRUE, and FALSE if all of the values in x are FALSE (including if there are no values). Otherwise the value is NA.



正如您所注意到的,这似乎与更常用的函数(例如 sum)的行为不同。和 mean ,因为这些函数的向量参数中存在 NA 值会返回 NA。这个感知上的问题被 joran's answer解决了这是指来自 ?Logic 的文档, 重新报价:

NA is a valid logical object. Where a component of x or y is NA, the result will be NA if the outcome is ambiguous. In other words NA & TRUE evaluates to NA, but NA & FALSE evaluates to FALSE. See the examples below.



因此,在歧义的情况下,例如,计算向量包含 NA 的均值,或 NA | FALSE其中缺失值可能为 TRUE,NA 将是输出。而在其他情况下,例如 any(c(TRUE, NA))TRUE | NA ,尽管存在缺失值,但结果是明确的。在@Floo0 的回答和对该问题的一些评论中,这种逻辑可能更清晰。

关于r - 为什么当没有真值时 any() 返回 NA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226525/

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