magrittr

March 16, 2015
By

(This article was first published on ipub » R, and kindly contributed to R-bloggers)

 

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

my.data %>% my.function

instead of

my.function(my.data)

For example:

mean(rnorm(100))

becomes:

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( 
  summarise(
    group_by( 
      filter(
        flights, 
        !is.na(dep_delay)
      ), 
      date, hour
    ), 
    delay = mean(dep_delay), 
    n = n()
  ), 
  n > 10 
)

With magrittr, this becomes:

hourly_delay <- flights %>% 
 filter(!is.na(dep_delay)) %>% 
 group_by(date, hour) %>% 
 summarise( 
   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.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

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)