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.data %>% my.function

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.