[This article was first published on ipub » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


F# made pipes popular among data scientists, and the magrittr package brought pipes to R. For example, with magrittr, you could write: %>% my.function

instead of


For example:



rnorm(100) %>% mean

or even (to take it to the extreme):

100 %>% rnorm %>% mean

This doesn’t look like a revolution, but in practice it makes things a lot easier, as the well-known example from Hadley Wickham shows: In traditional R, a data manipulation action might look like this:

hourly_delay <- filter( 
      date, hour
    delay = mean(dep_delay), 
    n = n()
  n > 10 

With magrittr, this becomes:

hourly_delay <- flights %>% 
 filter(! %>% 
 group_by(date, hour) %>% 
   delay = mean(dep_delay), 
   n = n() ) %>% 
 filter(n > 10)

The package also defines other operators, e.g. for assiging back to the original variable after the data manipulation. Here, however, I personally much prefer the classic R notation, e.g. along the lines of

mydataframe %>% op_1 %>% subset %>% filter %>% etc -> mydataframe

The key is the -> . The reason I like this a lot is that it keeps the flow of pipes: Take something, do something with it, and at the end assign it to a variable. The magrittr alternative would be the %<>% operator, which in my opinion is much less readable:

mydataframe %<>% op_1 %>% subset %>% filter %>% etc

By the way, the art-lovers will have guessed where the name magrittr comes from: French 20th century painter René Magritte painted the famous work “Ceci n’est pas une pipe” (this is not a pipe). We appreciate that Stefan Milton Bache, the author of magrittr, brought classic art to R, and forgive him the little typo in the vignette (pipe is féminine, i.e. une pipe, and not un pipe) ;-)


The post magrittr appeared first on ipub.

To leave a comment for the author, please follow the link and comment on their blog: ipub » R. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)