R中%&%;%函数是什么意思? [英] What does %>% function mean in R?

查看:1117
本文介绍了R中%&%;%函数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到在 dplyr等软件包中使用了%>%(大于百分比)功能 rvest .这是什么意思?是在R中编写闭包的一种方法吗?

I have seen the use of %>% (percent greater than percent) function in some packages like dplyr and rvest. What does it mean? Is it a way to write closure blocks in R?

推荐答案

%...%运算符

%>%没有内在的含义,但是用户(或程序包)可以随意定义%whatever%形式的运算符.例如,此函数将返回一个字符串,该字符串包含其左参数,后跟逗号和空格,然后是右参数.

%...% operators

%>% has no builtin meaning but the user (or a package) is free to define operators of the form %whatever% in any way they like. For example, this function will return a string consisting of its left argument followed by a comma and space and then it's right argument.

"%,%" <- function(x, y) paste0(x, ", ", y)

# test run

"Hello" %,% "World"
## [1] "Hello, World"

R的基数提供%*%(矩阵多重乘法),%/%(整数除法),%in%(rhs是否是rhs的组成部分),%o%(外部乘积)和%x%( kronecker产品).目前尚不清楚%%是否属于这一类,但它表示取模.

The base of R provides %*% (matrix mulitiplication), %/% (integer division), %in% (is lhs a component of the rhs?), %o% (outer product) and %x% (kronecker product). It is not clear whether %% falls in this category or not but it represents modulo.

expm R包expm定义了矩阵幂运算符%^%.有关示例,请参见 R中的矩阵幂.

expm The R package, expm, defines a matrix power operator %^%. For an example see Matrix power in R .

operators 运算符R包已定义了大量此类运算符,例如%!in%(不适用于%in%).参见 http://cran.r-project.org/web/packages/operator/operators.pdf

operators The operators R package has defined a large number of such operators such as %!in% (for not %in%). See http://cran.r-project.org/web/packages/operators/operators.pdf

magrittr 对于%>%,magrittr R软件包已按照magrittr小插图中的定义对其进行了定义.参见 http://cran.r-project.org/web/包/magrittr/vignettes/magrittr.html

magrittr In the case of %>% the magrittr R package has defined it as discussed in the magrittr vignette. See http://cran.r-project.org/web/packages/magrittr/vignettes/magrittr.html

magittr还定义了许多其他这样的运算符.请参阅先前链接的其他管道操作员"部分,其中讨论了%T>%%<>%%$%

magittr has also defined a number of other such operators too. See the Additional Pipe Operators section of the prior link which discusses %T>%, %<>% and %$% and http://cran.r-project.org/web/packages/magrittr/magrittr.pdf for even more details.

dplyr 用于定义%.%运算符的dplyr R软件包,该软件包相似.但是,它已被弃用,并且dplyr现在建议用户使用%>%,该dplyr从magrittr导入,并可供dplyr用户使用.正如David Arenburg在评论中提到的那样,此SO问题讨论了它与magrittr的%>%之间的区别: %.%(dplyr)和%>%(magrittr)之间的差异

dplyr The dplyr R package used to define a %.% operator which is similar; however, it has been deprecated and dplyr now recommends that users use %>% which dplyr imports from magrittr and makes available to the dplyr user. As David Arenburg has mentioned in the comments this SO question discusses the differences between it and magrittr's %>% : Differences between %.% (dplyr) and %>% (magrittr)

pipeR R程序包pipeR定义了一个%>>%运算符,该运算符类似于magrittr的%>%,并且可以替代它.参见 http://renkun.me/pipeR-tutorial/

pipeR The R package, pipeR, defines a %>>% operator that is similar to magrittr's %>% and can be used as an alternative to it. See http://renkun.me/pipeR-tutorial/

pipeR包也定义了许多其他这样的运算符.请参阅: http://cran.r-project.org/web/packages/pipeR/pipeR.pdf

The pipeR package also has defined a number of other such operators too. See: http://cran.r-project.org/web/packages/pipeR/pipeR.pdf

postlogic .postlogic程序包定义了%if%%unless%运算符.

postlogic The postlogic package defined %if% and %unless% operators.

wrapr R包,包装程序,定义了一个点管道%.>%,它是%>%的显式版本,因为它不隐式插入参数,而仅替代点的显式使用在右手侧.这可以被认为是%>%的另一种选择.参见 https://winvector.github.io/wrapr/articles/dot_pipe.html

wrapr The R package, wrapr, defines a dot pipe %.>% that is an explicit version of %>% in that it does not do implicit insertion of arguments but only substitutes explicit uses of dot on the right hand side. This can be considered as another alternative to %>%. See https://winvector.github.io/wrapr/articles/dot_pipe.html

Bizarro管.这实际上不是管道,而是一些聪明的基本语法,以类似于管道的方式工作,而无需实际使用管道.在 http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/的想法是而不是写:

Bizarro pipe. This is not really a pipe but rather some clever base syntax to work in a way similar to pipes without actually using pipes. It is discussed in http://www.win-vector.com/blog/2017/01/using-the-bizarro-pipe-to-debug-magrittr-pipelines-in-r/ The idea is that instead of writing:

1:8 %>% sum %>% sqrt
## [1] 6

一个写以下内容.在这种情况下,我们显式使用dot而不是省略dot参数,并在管道的每个组件结尾分配一个名称为dot(.)的变量.我们在后面加上分号.

one writes the following. In this case we explicitly use dot rather than eliding the dot argument and end each component of the pipeline with an assignment to the variable whose name is dot (.) . We follow that with a semicolon.

1:8 ->.; sum(.) ->.; sqrt(.)
## [1] 6

更新在顶部添加了有关expm软件包的信息和简化示例.添加了后逻辑程序包.

Update Added info on expm package and simplified example at top. Added postlogic package.

这篇关于R中%&%;%函数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆