%.% (dplyr) 和 %>% (magrittr) 之间的差异 [英] Differences between %.% (dplyr) and %>% (magrittr)
问题描述
dplyr
包引入了 %.%
操作符将左侧作为右侧函数的参数传递,类似于 *NIX 管道.magrittr
包是一个更轻量级的包,它的存在仅用于定义类似管道的运算符.
The dplyr
package introduced the %.%
operator to pass the left hand side as an argument of the function on the right hand side, similar to a *NIX pipe. The magrittr
package is a much more lightweight package that exists to define only that pipe-like operator.
然而一个使用 %.%
和另一个 %>%
.
Yet one uses %.%
and the other %>%
.
两者有什么区别吗?我可以只在 dplyr
代码中使用 %>%
,否则会导致细微的错误吗?
Is there any difference between the two? Can I just use %>%
even in dplyr
code, or will that cause subtle bugs?
在检查代码时,它们在早期采用了截然不同的路径,因此简单的眼球比较就会表明它们是不同的.当我在网上搜索它们时,我找不到任何记录在案的内容,也没有在文档本身中找到任何内容.
On inspecting the code, they take very different paths early on, so simple eyeball comparison would suggest that they're different. I haven't been able to find anything documented when I search the web for them, nor have I run across anything in the documentation itself.
推荐答案
dplyr 现在从 magrittr 导入 %>%
并默认使用它.有关详细信息,请参阅此答案.
dplyr now imports %>%
from magrittr and uses it by default. See this answer for details.
差异包括
您可以使用
.
作为左侧的占位符,例如
you can use a
.
as placeholder for the left-hand side, e.g.
iris %>% plot(Sepal.Length ~ Sepal.Width, data = .)
%>%
尊重(rhs)
,例如
1:10 %>% (call("sum"))
1:10 %>% (function(x) x^2 + 2*x)
为了更有用这个例子,见
https://gist.github.com/anonymous/0c69b019d0b4f6ae5050
For a more useful
example of this, see
https://gist.github.com/anonymous/0c69b019d0b4f6ae5050
对于单参数函数调用,可以省略括号:
For single argument function calls, you can omit parens:
"2014-05-18" %>% as.Date
这篇关于%.% (dplyr) 和 %>% (magrittr) 之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!