class: center, middle, inverse, title-slide # Data wrangling ## The pipe ### Ben Baumer ### SDS 192Sep 28, 2020(
http://beanumber.github.io/sds192/lectures/mdsr_wrangling_02-pipe.html
) --- class: center, middle, inverse # The pipe --- ## The pipe operator .pull-left[ .center[![](http://hexb.in/hexagons/magrittr.png)] - Inspired by pipe (`|`) in UNIX - Provided by `magrittr` package ] -- .pull-right[ ![](https://upload.wikimedia.org/wikipedia/en/b/b9/MagrittePipe.jpg) - [The Treachery of Images](https://en.wikipedia.org/wiki/The_Treachery_of_Images) - Rene Magritte, 1929 ] --- ## How does the pipe work? ![](../gfx/tidy-pipe.png) --- ## Using the pipe The expression ```r mydata %>% verb(arguments) ``` is the same as: ```r verb(mydata, arguments) ``` -- Thus, ```r function(x, args) ``` -- has the same effect as ```r x %>% function(args) ``` --- ## Why the pipe? Instead of having to read/write: ```r select(filter(mutate(data, args1), args2), args3) ``` -- You can write: ```r data %>% mutate(args1) %>% filter(args2) %>% select(args3) ``` --- ## Little Bunny Foo Foo <iframe width="640" height="360" src="https://www.youtube.com/embed/R6xKM-H2awE?ecver=1" frameborder="0" allowfullscreen></iframe> --- ## Coding Little Bunny Foo Foo - Nested form: ```r bop(scoop(hop(foo_foo, through = forest), up = field_mice), on = head) ``` -- - With pipes: ```r foo_foo %>% hop(through = forest) %>% scoop(up = field_mouse) %>% bop(on = head) ``` .footnote[https://github.com/hadley/r4ds/blob/master/pipes.Rmd]