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

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

问题描述

我已经看到在诸如 dplyrrvest.这是什么意思?有没有办法在 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?

推荐答案

%...% operators

%>% 没有内置含义,但用户(或包)可以以他们喜欢的任何方式自由定义 %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%(是lhs是rhs的一个组成部分?),%o%(外积)和%x%(克罗内克积).%% 是否属于这一类尚不清楚,但它代表模数.

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%(用于 not %in%).参见 http://cran.r-project.org/web/packages/运营商/运营商.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

igraph 这个包定义了 %--% 、 %->% 和 %<-% 来选择边.

igraph This package defines %--% , %->% and %<-% to select edges.

lubridate 这个包定义了 %m+% 和 %m-% 来添加和减去月份,以及 %--% 来定义一个间隔.igraph 还定义了 %--% .

lubridate This package defines %m+% and %m-% to add and subtract months and %--% to define an interval. igraph also defines %--% .

ma​​grittr%>% 的情况下,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>%%<>%%$%http://cran.r-project.org/web/packages/magrittr/magrittr.pdf 了解更多详情.

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 包 wrapr 定义了一个点管 %.>%,它是 %>% 的显式版本因为它不会隐式插入参数,而只是在右侧替换显式使用点.这可以被认为是 %>% 的另一种替代方法.请参阅 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

奇异的管道.这不是真正的管道,而是一些巧妙的基本语法,可以在不实际使用管道的情况下以类似于管道的方式工作.它在 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 包和简化示例的信息.添加了 postlogic 包.

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

更新 2 R 的开发版本定义了一个 |> 管道.与 magrittr 的 %>% 不同,它只能代入右侧的第一个参数.虽然有限,但它通过语法转换工作,因此不会影响性能.

Update 2 The development version of R has defined a |> pipe. Unlike magrittr's %>% it can only substitute into the first argument of the right hand side. Although limited, it works via syntax transformation so it has no performance impact.

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

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