1

I am an entry level R user.May be this question sound like easy but it will be great if some one can help . what is the meaning of this symbol in R-coding ...

  %>%

Thank you

nopainnogain
  • 123
  • 1
  • 2
  • 10
  • 1
    See [this page](http://cran.at.r-project.org/web/packages/magrittr/vignettes/magrittr.html) – Kara Woo Jul 24 '14 at 18:14
  • There is no definition for `%>%` in base R. It is an infix operator defined by a package likely. Be clear what package you are using. – MrFlick Jul 24 '14 at 18:14
  • @MrFlick: Let's say if i dont have any package which use this symbol then what is the other substitute which can allow me to use the code containing %>% – nopainnogain Jul 24 '14 at 18:18
  • @nopainnogain The '%>%' is simply undefined in base R. You will get an error if you use it. It doesn't mean anything. There are package (such as `magrittr` that give it special meaning. While `magrittr` is perhaps the most common, any package or any user is free to define it how ever they like. – MrFlick Jul 24 '14 at 18:21

2 Answers2

11

%>% is most commonly used as an operator for the popular dplyr package

It can be used to chain code together. It is very useful when you are performing several operations on data, and don’t want to save the output at each intermediate step.

Varun
  • 5
  • 3
Vlo
  • 3,168
  • 13
  • 27
10

%>% means whatever you want it to mean, in Base R anyway:

> %>%
Error: unexpected SPECIAL in "%>%"

(which means that symbol is not defined.)

Binary operators are ones that have an input from the left and from the right of the operator, just like *, + etc. You use them as you would mathematically like a * b, which R turns into the call '*'(a, b). R allows you to add your own binary operators via the %foo% syntax, with foo replace by whatever you want, as long as it hasn't already been used by R, which includes %*% and %/% for example.

`%foo%` <- function(x, y) paste("foo", x, "and foo", y)

> 1 %foo% 2
[1] "foo 1 and foo 2"

%>% takes on a specific and well-defined meaning once you load the magrittr R package for example, where it is used as a pipe operator might be in a Unix shell to chain together a series of function calls.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453