Search code examples
rprogramming-languagessymbols

Meaning of Symbol %>% in R


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


Solution

  • %>% 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.