What does %>% function mean in R?

475,838

Solution 1

%...% operators

%>% has no builtin meaning but the user (or a package) is free to define operators of the form %whatever% in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument.

"%,%" <- function(x, y) paste0(x, ", ", y)

# test run

"Hello" %,% "World"
## [1] "Hello, World"

The base of R provides %*% (matrix mulitiplication), %/% (integer division), %in% (is lhs a component of the rhs?), %o% (outer product) and %x% (kronecker product). It is not clear whether %% falls in this category or not but it represents modulo.

expm The R package, expm, defines a matrix power operator %^%. For an example see Matrix power in R .

operators The operators R package has defined a large number of such operators such as %!in% (for not %in%). See http://cran.r-project.org/web/packages/operators/operators.pdf

igraph This package defines %--% , %->% and %<-% to select edges.

lubridate This package defines %m+% and %m-% to add and subtract months and %--% to define an interval. igraph also defines %--% .

Pipes

magrittr In the case of %>% the magrittr R package has defined it as discussed in the magrittr vignette. See http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html

magittr has also defined a number of other such operators too. See the Additional Pipe Operators section of the prior link which discusses %T>%, %<>% and %$% and http://cran.r-project.org/web/packages/magrittr/magrittr.pdf for even more details.

dplyr The dplyr R package used to define a %.% operator which is similar; however, it has been deprecated and dplyr now recommends that users use %>% which dplyr imports from magrittr and makes available to the dplyr user. As David Arenburg has mentioned in the comments this SO question discusses the differences between it and magrittr's %>% : Differences between %.% (dplyr) and %>% (magrittr)

pipeR The R package, pipeR, defines a %>>% operator that is similar to magrittr's %>% and can be used as an alternative to it. See http://renkun.me/pipeR-tutorial/

The pipeR package also has defined a number of other such operators too. See: http://cran.r-project.org/web/packages/pipeR/pipeR.pdf

postlogic The postlogic package defined %if% and %unless% operators.

wrapr The R package, wrapr, defines a dot pipe %.>% that is an explicit version of %>% in that it does not do implicit insertion of arguments but only substitutes explicit uses of dot on the right hand side. This can be considered as another alternative to %>%. See https://winvector.github.io/wrapr/articles/dot_pipe.html

Bizarro pipe. This is not really a pipe but rather some clever base syntax to work in a way similar to pipes without actually using pipes. It is discussed in http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/ The idea is that instead of writing:

1:8 %>% sum %>% sqrt
## [1] 6

one writes the following. In this case we explicitly use dot rather than eliding the dot argument and end each component of the pipeline with an assignment to the variable whose name is dot (.) . We follow that with a semicolon.

1:8 ->.; sum(.) ->.; sqrt(.)
## [1] 6

Update Added info on expm package and simplified example at top. Added postlogic package.

Update 2 The development version of R has defined a |> pipe. Unlike magrittr's %>% it can only substitute into the first argument of the right hand side. Although limited, it works via syntax transformation so it has no performance impact.

Solution 2

%>% is similar to pipe in Unix. For example, in

a <- combined_data_set %>% group_by(Outlet_Identifier) %>% tally()

the output of combined_data_set will go into group_by and its output will go into tally, then the final output is assigned to a.

This gives you handy and easy way to use functions in series without creating variables and storing intermediate values.

Solution 3

My understanding after reading the link offered by G.Grothendieck is that %>% is an operator that pipes functions. This helps readability and productivity as it's easier to follow the flow of multiple functions through these pipes than going backwards when multiple function are nested.

Share:
475,838

Related videos on Youtube

alfakini
Author by

alfakini

I am a hacker and technologist. I love to work on the bleeding edge, rapidly iterating to find innovative solutions. I enjoy working with other people and sharing knowledge. Founder and executive director of Magrathea Labs, where I work solving challenging problems using technology. Currently, we're focused on bringing digital transformation to the health industry. Founder and president of the Fab Lab Joinville maker community. We're developing the Makers Network, an online community of +600 tech professionals where we host tech events and develop open-source software for the social good. I have an MSc. in Computer Engineering from the University of São Paulo, where I developed research on machine learning and bioinformatics. I studied Computer Science at UDESC, where I studied topics on machine learning, digital signal processing and open-source software development. Topics of interest: team leading, human communities, systems thinking, entrepreneurship, web 3, token economics, blockchain, software engineering, data-intensive computing, and programming languages.

Updated on January 24, 2022

Comments

  • alfakini
    alfakini over 2 years

    I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest. What does it mean? Is it a way to write closure blocks in R?

    • jbaums
      jbaums over 9 years
      or look at ?'%>%'
    • alfakini
      alfakini over 9 years
      Thanks David! jbaums, unfortunately there is no doc about this. ?'%>%' retuns "No documentation for '%>%' in specified packages and libraries"
    • David Arenburg
      David Arenburg over 9 years
      @alf. you need to library(magrittr) or library(dplyr) first and then run ?'%>%', though my link provides more informaiton
    • David Arenburg
      David Arenburg over 9 years
      Also, take a look here
    • Carl Witthoft
      Carl Witthoft over 9 years
      Helpful hint: get the sos package
    • Joshua Ulrich
      Joshua Ulrich over 8 years
    • W Barker
      W Barker almost 6 years
      @DavidArenburg, I'm trying to understand %>% in relation to an answer to a question I recently posed (stackoverflow.com/questions/50492862/…); got to this page and tried to access your link (first comment), but it returns the dreaded "404". From other comments, it sounds like it was helpful; can you repost?
    • David Arenburg
      David Arenburg almost 6 years
      @WBarker look it up in here, page #9
  • pluke
    pluke almost 8 years
    I also see: %<>%, %T>%, %$%, what do they do? rpackages.ianhowson.com/cran/magrittr/man/pipe.html
  • G. Grothendieck
    G. Grothendieck almost 8 years
    From within R after loading magrittr get help using: ?"%<>%", etc.
  • Christopher Stephan
    Christopher Stephan over 6 years
    The advantages you mentioned are demonstrated with code examples here.
  • merv
    merv about 5 years
    Seems like this is already well-explained in stackoverflow.com/a/27129032/570918.