Search code examples
rdplyrpipingmagrittr

how to write the following code using %>% in R


I am trying to use more and more of the %>% operator with dplyr in my code but I find I am not able figure out how to use %>% all the time. E.g., how would I use it with the complete.cases() function in a correct way for

X <- X[complete.cases(X), ]

using the %>% operator. I am writing

X %>% filter(X %>% complete.cases)

but having X on both sides of the operator does not seem to be the right way to me. The code works though!


Solution

  • It will just be

    x %>% filter(complete.cases)
    

    The reason for using the chaining operator is to avoid having to type the data each time, so even if you have more functions, you can just skip the first parameter and mention the rest.