How to use logical functions with %>% operator (dplyr)

I think it's just an operator precedence issue. The %>% operator has lower precedence than other operators such as ==, so what you're actually doing with aaa == 4 %>% which is passing 4 to the which function ...

(aaa == 4) %>% which

seems to work just fine ...

This use of %>% seems a little odd/unnecessary, but maybe that's just because you've isolated it from your workflow in order to create a reproducible example. Maybe you could say a little more about the context?


Use brackets around logical operations.

aaa <- sample(1:5, 10, replace = TRUE)
(aaa == 4) %>% which(.)
[1]  7  9 10
(aaa == 4) %>% which()
[1]  7  9 10
(aaa == 4) %>% any()
[1] TRUE

Tags:

Logic

R

Dplyr