Skip to content

Instantly share code, notes, and snippets.

@datalove
Last active August 29, 2015 14:10
Show Gist options
  • Select an option

  • Save datalove/7e3bbb987de2394907ee to your computer and use it in GitHub Desktop.

Select an option

Save datalove/7e3bbb987de2394907ee to your computer and use it in GitHub Desktop.
Using magrittr 1.5's functional sequences to create reusable chunks of dplyr code.
# Let's say we're doing some analysis on the mtcars data..
# displacement/cylinder for high horsepower cars
mtcars %>%
filter(hp > 100) %>%
mutate(disp_cyl = disp/cyl) %>% filter(disp_cyl > 25) %>%
group_by(cyl,am) %>% summarise(mean_hp = mean(hp), mean_disp_cyl = mean(disp_cyl))
# or displacement/cylinder for all cars
mtcars %>%
mutate(disp_cyl = disp/cyl) %>% filter(disp_cyl > 25) %>%
group_by(cyl,am) %>% summarise(mean_hp = mean(hp), mean_disp_cyl = mean(disp_cyl))
# That's beautiful! Thanks dplyr!
# Do magrittr functional sequences let me create resuable chunks of dplyr that I can chain together?
step1 <- . %>% filter(hp > 100)
step2 <- . %>% mutate(disp_cyl = disp/cyl) %>% filter(disp_cyl > 25)
step3 <- . %>% group_by(cyl,am) %>% summarise(mean_hp = mean(hp), mean_disp_cyl = mean(disp_cyl))
# displacement/cylinder for high horsepower cars?
mtcars %>% step1 %>% step2 %>% step3
# displacement/cylinder for all cars (i.e. leave out step 1)
mtcars %>% step2 %>% step3
# Turns out that yes, magrittr functionals can create reusable chunks dplyr commands!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment