0

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!

Satya
  • 1,708
  • 1
  • 15
  • 39

3 Answers3

2
library(dplyr)
mtcars %>%
filter(complete.cases(.))
user227710
  • 3,164
  • 18
  • 35
2

As this post also describes. A period points to the dataframe being passed in, allowing you to do this:

X <- data.frame(a=rep(1,10),
                b=rep("a", 10),
                c=1:10)
X[sample(1:20,5),2:3] <- NA

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

The idea is that the data frame that results from the left side of the %>% operator will be the first input in the function call after it.

Community
  • 1
  • 1
MattV
  • 1,353
  • 18
  • 42
1

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.

ytk
  • 2,787
  • 4
  • 27
  • 42
  • Here: `library(datasets) library(dplyr) x %>% subset(mpg > 20) %>% group_by(gear) %>% summarise(avg_hp = mean(hp))` will give this output: `gear avg_hp 1 3 103.5 2 4 82.8 3 5 102.0` – ytk May 22 '15 at 22:40