Reputation: 11
How can I use the pipe %>%
with logical chains without having to put {}
around everything?
Well this example is pretty basic, but if you chain multiple logic expressions it gets really nasty with all the bracketing, so I want to avoid brackets at all costs.
rep(T,10) & rep(F,10) %>% sum
expected:
0
what I got:
[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Upvotes: 0
Views: 571
Reputation: 66834
The pipe has higher precedece than &
. Force the logical operation first by enclosing in brackets.
(rep(T,10) & rep(F,10)) %>% sum
[1] 0
See ?Syntax
for a list of operator precedence.
If you must avoid brackets, you will need to use an operator with equal or higher precedence (e.g. another pipe) and the functional form of the logical operators:
rep(T,10) %>% and(rep(F,10)) %>% sum
[1] 0
Upvotes: 6