r - 使用 magrittr 和否定运算符时结果不一致

标签 r logical-operators magrittr

<分区>

> v <- c(1,2,NA,5)
> is.na(v)
[1] FALSE FALSE  TRUE FALSE
> !is.na(v)
[1]  TRUE  TRUE FALSE  TRUE
> 
> !is.na(v) %>% all()
[1] TRUE
> all(!is.na(v))
[1] FALSE
> (!is.na(v)) %>% all()
[1] FALSE

在没有括号的情况下,%>% 将 all() 应用于 is.na(v),然后应用 ! 运算符.为什么它在这里有这样的操作顺序,我应该厌倦哪些其他功能/操作符?

最佳答案

magrittr 提供了一个 set of operators它的链接效果更好。所以,你可以使用

not(is.na(v)) %>% all()

建议是

...special attention is advised when using non-magrittr operators in a pipe-chain (+, -, $, etc.), as operator precedence will impact how the chain is evaluated. In general it is advised to use the aliases provided by magrittr.

关于r - 使用 magrittr 和否定运算符时结果不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490088/

相关文章:

r - tidyverse:一个变量与 data.frame 中所有其他变量的交叉表

r - 如何在 R 中使用 pdf 和 mfrow

c - C 中带算术运算符的逻辑运算符

conditional - 我可以简化这个使用逻辑否定运算符的条件语句吗?

javascript - 如果输入 a 或 b 正确/正确,我如何执行操作?

r - 如何使用 magrittr 从数据框中提取单个元素?

r - 为什么运行 `f <- function(x){x} %>% f` 不会抛出错误?

R markdown 代码显示我不想要的输出,如何删除它?

SQL 查询 : same rows

r - 如何编写自定义管道友好函数?