Negation of `!` In the dplyr pipeline `%>%`

Can negation be used in a dplyr pipeline?

For example, for

df = data.frame(a = c(T,F,F), b = c(T,T,T)) 

I can write

 !df 

but i can't write

 df %>% ! 

(as ! not a function).

In particular, I use !is.na , but I cannot include it in pipelines.

+6
source share
2 answers

You can use backticks around !

  df %>% `!` # ab #[1,] FALSE FALSE #[2,] TRUE FALSE #[3,] TRUE FALSE 

For !is.na

  df$a[2] <- NA df %>% is.na %>% `!` # ab #[1,] TRUE TRUE #[2,] FALSE TRUE #[3,] TRUE TRUE 
+11
source

Note that the pipeline operator used in dplyr is imported from magrittr , so use other functions to access

 library(magrittr) 

See the ?extact for a list of common aliases suitable for magrittr.

In this case, not() is defined as an alias for !

 df %>% not 

To simplify the challenge! is.na, you can define

 not_ <- function(x, f) not(f(x)) df %>% not_(is.na) 
+9
source

Source: https://habr.com/ru/post/986930/


All Articles