Using dplyr group_by in a function

别说谁变了你拦得住时间么 提交于 2019-12-07 12:14:42

问题


I am trying to use dplyr's group_by in a local function, example:

testFunction <- function(df, x) {
  df %>%
group_by(x) %>%
summarize(mean.Petal.Width = mean(Petal.Width))
}

testFunction(iris, Species)

and I get an error "... unknown variable to group by: x" I've tried group_by_ and it gives me a summary of the entire dataset. Anybody have a clue how I can fix this?

Thanks in advance!


回答1:


Here is one way to work with the new enquo from dplyr, where enquo takes the string and converts to quosure which gets evaluated by unquoting (UQ or !!) in group_by, mutate, summarise etc.

library(dplyr)
testFunction <- function(df, x) {
 x <- enquo(x)
  df %>%
    group_by(!! x) %>%
     summarize(mean.Petal.Width = mean(Petal.Width))
 }

testFunction(iris, Species)
# A tibble: 3 x 2
#     Species mean.Petal.Width
#      <fctr>            <dbl>
#1     setosa            0.246
#2 versicolor            1.326
#3  virginica            2.026



回答2:


I got it to work like this:

testFunction <- function(df, x) {
                      df %>%
                         group_by(get(x)) %>%
                         summarize(mean.Petal.Width = mean(Petal.Width))
                 }

testFunction(iris,"Species")

I changed x to get(x), and Species to "Species" in testFunction(iris,...).



来源:https://stackoverflow.com/questions/44659943/using-dplyr-group-by-in-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!