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

In the absence of parenthesis, %>% is applying all() to is.na(v) and then applying ! operator. Why does it have such order of operation here, and for what other functions/operators should I be weary of this?

chungkim271
  • 927
  • 1
  • 10
  • 20

1 Answers1

2

magrittr provides a set of operators that work better with its chaining. So, you can use

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

Advice is

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

dommer
  • 19,610
  • 14
  • 75
  • 137