Is it possible to use negation in dplyr pipeline?
E.g for
df = data.frame(a = c(T,F,F), b = c(T,T,T))
I can write
!df
but I cannot write
df %>% !
(as ! is not a function).
In particular, I use !is.na a lot, but I am not able to incorporate it into pipelines.
You can use backticks around !
 df %>%
       `!`
 #      a     b
 #[1,] FALSE FALSE
 #[2,]  TRUE FALSE
 #[3,]  TRUE FALSE
For !is.na
 df$a[2] <- NA
 df %>% 
      is.na %>% 
      `!`
 #       a    b
 #[1,]  TRUE TRUE
 #[2,] FALSE TRUE
 #[3,]  TRUE TRUE
Note that the piping operator used in dplyr is imported from magrittr so for access to the other functions, use 
library(magrittr)
See the ?extact page for a list of common magrittr-friendly aliases.
In this case not() is defined as a alias for !
df %>% not
To make it easier to call !is.na, you could define
not_ <- function(x, f) not(f(x))
df %>% not_(is.na)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With