dplyr:如何在函数中使用group_by? [英] dplyr: How to use group_by inside a function?
问题描述
我想在另一个函数中使用 dplyr :: group_by
函数,但是我不知道如何将参数传递给这个函数。
有人可以提供一个工作示例吗?
library(dplyr)
data (iris)
iris%。%group_by(Species)%。%summarize(n = n())#
##来源:本地数据框[3 x 2]
##种n
## 1 virginica 50
## 2 versicolor 50
## 3 setosa 50
mytable0< - function(x,...)x% 。%group_by(...)%。%summary(n = n())
mytable0(iris,Species)#OK
##来源:本地数据帧[3 x 2]
##物种n
## 1 virginica 50
## 2 versicolor 50
## 3 setosa 50
mytable1< - function(x, key)x%。%group_by(as.name(key))%。%summarize(n = n())
mytable1(iris,Species)#错误!
#错误:列'as.name(key)'(SYMSXP)的不受支持的类型
mytable2< - function(x,key)x%。%group_by(key)%。 %summary(n = n())
mytable2(虹膜,物种)#错误!
#错误:index out of bounds
对于编程, group_by _
是 group_by
的对应方:
library(dplyr)
mytable< - function(x,...)x%>%group_by _(...)%>%summary (n = n())
mytable(iris,Species)
#或iris%>%mytable(Species)
其中:
物种n
1 setosa 50
2 versicolor 50
3 virginica 50
更新在这个时候,这个书写的dplyr使用%。%
这是上面最初使用的,但现在%>%
被赞成,所以改变了以上,以保持这个相关。
更新2 重新组合现在已被弃用,请改用group_by_。 p>
更新3 group_by_(list(...))
现在变成 GROUP_BY _(...)
在新版本的dplyr中按照Roberto的评论。
更新4 在评论中添加了小的变体。
I want to use use the dplyr::group_by
function inside another function, but I do not know how to pass the arguments to this function.
Can someone provide a working example?
library(dplyr)
data(iris)
iris %.% group_by(Species) %.% summarise(n = n()) #
## Source: local data frame [3 x 2]
## Species n
## 1 virginica 50
## 2 versicolor 50
## 3 setosa 50
mytable0 <- function(x, ...) x %.% group_by(...) %.% summarise(n = n())
mytable0(iris, "Species") # OK
## Source: local data frame [3 x 2]
## Species n
## 1 virginica 50
## 2 versicolor 50
## 3 setosa 50
mytable1 <- function(x, key) x %.% group_by(as.name(key)) %.% summarise(n = n())
mytable1(iris, "Species") # Wrong!
# Error: unsupported type for column 'as.name(key)' (SYMSXP)
mytable2 <- function(x, key) x %.% group_by(key) %.% summarise(n = n())
mytable2(iris, "Species") # Wrong!
# Error: index out of bounds
For programming, group_by_
is the counterpart to group_by
:
library(dplyr)
mytable <- function(x, ...) x %>% group_by_(...) %>% summarise(n = n())
mytable(iris, "Species")
# or iris %>% mytable("Species")
which gives:
Species n
1 setosa 50
2 versicolor 50
3 virginica 50
Update At the time this was written dplyr used %.%
which is what was originally used above but now %>%
is favored so have changed above to that to keep this relevant.
Update 2 regroup is now deprecated, use group_by_ instead.
Update 3 group_by_(list(...))
now becomes group_by_(...)
in new version of dplyr as per Roberto's comment.
Update 4 Added minor variation suggested in comments.
这篇关于dplyr:如何在函数中使用group_by?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!