问题
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