13.13 Chaining with dplyr

  • Code (e.g. above) is sometimes difficult to read and the arguments are very long
  • With dplyr it is possible to use the %>% operator
    • x %>% f(y) turns into f(x, y): x (e.g. a data set) is inserted into the function on the right of %>%
    • %>%: Forces > to be a function which does the above
  • The resulting code seems a bit more intuitive


swiss %>% # Stick swiss into the functions below consecutively
select(Fertility, Infant.Mortality, Examination) %>%
filter(Fertility > 82) %>% 
filter(Infant.Mortality < 24) 

# DO TRY THAT AT HOME!!!