Meaning of Symbol %>% in R [duplicate]

纵饮孤独 提交于 2019-12-10 00:13:05

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/24941080/meaning-of-symbol-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!