Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between %>% and %,% in magrittr?

Tags:

r

piping

magrittr

Github developmental version of magrittr includes some cool new function for piping but I do not exactly catch de difference between %>% and %,%. Is this only formal with %>% for value and %,% for functions, or there is some specific peculiarity?

like image 904
BBrill Avatar asked Aug 08 '14 20:08

BBrill


People also ask

Is %>% the same as in R?

%>% is called the forward pipe operator in R. It provides a mechanism for chaining commands with a new forward-pipe operator, %>%. This operator will forward a value, or the result of an expression, into the next function call/expression. It is defined by the package magrittr (CRAN) and is heavily used by dplyr (CRAN).

What is Magrittr pipe?

The magrittr pipe operators use non-standard evaluation. They capture their inputs and examines them to figure out how to proceed. First a function is produced from all of the individual right-hand side expressions, and then the result is obtained by applying this function to the left-hand side.

What is %>% used for?

The compound assignment %<>% operator is used to update a value by first piping it into one or more expressions, and then assigning the result. For instance, let's say you want to transform the mpg variable in the mtcars data frame to a square root measurement.

What package is %>% a part of in R?

The R packages dplyr and sf import the operator %>% from the R package magrittr.


1 Answers

The normal piping operator is %>%. You can use %,% to create a reusable pipe, a pipe without data. Then later you can use the same pipe with various data sets. Here is an example.

library(magrittr)
library(dplyr)
library(Lahman)

Suppose you want to calculate the top 5 baseball players, according to total hits. Then you can do something like this (taken from the magrittr README):

Batting %>%
   group_by(playerID) %>%
   summarise(total = sum(G)) %>%
   arrange(desc(total)) %>%
   head(5)
# Source: local data frame [5 x 2]
# 
#    playerID total
# 1  rosepe01  3562
# 2 yastrca01  3308
# 3 aaronha01  3298
# 4 henderi01  3081
# 5  cobbty01  3035

So far so good. Now let's assume that you have several data sets in the same format as Batting, so you could just reuse the same pipe again. %,% helps you create, save and reuse the pipe:

top_total <- group_by(playerID) %,%
   summarise(total = sum(G)) %,%
   arrange(desc(total)) %,%
   head(5)

top_total(Batting)
# Source: local data frame [5 x 2]
# 
#    playerID total
# 1  rosepe01  3562
# 2 yastrca01  3308
# 3 aaronha01  3298
# 4 henderi01  3081
# 5  cobbty01  3035

Of course you could also create a function the regular R way, i.e. top_total <- function(...) ..., but %,% is a more concise way.

like image 60
Gabor Csardi Avatar answered Oct 14 '22 23:10

Gabor Csardi