53

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
4

4 Answers 4

78

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.

Update 5: With rlang/tidyeval it is now possible to do this:

library(rlang)
mytable <- function(x, ...) {
  group_ <- syms(...)
  x %>% 
    group_by(!!!group_) %>% 
    summarise(n = n())
}
mytable(iris, "Species")

or passing Species unevaluated, i.e. no quotes around it:

library(rlang)
mytable <- function(x, ...) {
  group_ <- enquos(...)
  x %>% 
    group_by(!!!group_) %>% 
    summarise(n = n())
}
mytable(iris, Species)

Update 6: There is now a {{...}} notation that works if there is just one grouping variable:

mytable <- function(x, group) {
  x %>% 
    group_by({{group}}) %>% 
    summarise(n = n())
}
mytable(iris, Species)
4
  • 2
    There is no need for the as.name etc. mytable <- function(x, ...) x %.% regroup(value = list(...)) %.% summarise(n = n()); mytable(x,'key1','key2') , or mytable <- function(x, key) x %.% regroup(value = as.list(key) %.% summarise(n = n()); mytable(x, list('key1','key2'))` to allow grouping by more than one column
    – mnel
    Commented Feb 17, 2014 at 2:17
  • Thanks. Have replaced as.name(key) with ... . Commented Feb 17, 2014 at 4:30
  • 7
    Now it's even simpler in dplyr 0.3 with lazyeval: mytable <- function(x, ...) x %>% group_by_(...) %>% summarise(n = n())
    – Roberto
    Commented Nov 12, 2014 at 1:53
  • 1
    @Roberto's solution can be rephrased using magrittr's notion of functional sequence: With mytable <- function(...) . %>% group_by_(...) %>% summarise(n = n()), mytable("Species") gives a functional sequence, so iris %>% mytable("Species")() gives the desired summary. (Note that the calling () is necessary—a minor "wart.") Of course, whether this extra degree of decoupling is worthwhile will depend on the specific application.
    – egnha
    Commented Aug 27, 2016 at 9:07
13

UPDATE: As of dplyr 0.7.0 you can use tidy eval to accomplish this.

See http://dplyr.tidyverse.org/articles/programming.html for more details.

library(tidyverse)
data("iris")

my_table <- function(df, group_var) {
  group_var <- enquo(group_var)      # Create quosure
  df %>% 
    group_by(!!group_var) %>%        # Use !! to unquote the quosure
    summarise(n = n())
}

my_table(iris, Species)

> my_table(iris, Species)
# A tibble: 3 x 2
     Species     n
      <fctr> <int>
1     setosa    50
2 versicolor    50
3  virginica    50
5
  • Doesn't n() give you a note when doing a check?
    – Elin
    Commented Nov 28, 2017 at 16:53
  • Hi @Elin. I'm not exactly sure what you mean. I just reran this code and didn't see any notes. Commented Nov 28, 2017 at 18:09
  • In a package build check?
    – Elin
    Commented Nov 28, 2017 at 20:38
  • I see. I hadn't tried including this in a package. Does just using dply::n() resolve the note? Commented Nov 28, 2017 at 20:41
  • No. I think you may need to quote then unquote the whole expression.
    – Elin
    Commented Nov 28, 2017 at 21:52
8

As a complement to the Update 6 in the answer by @G. Grothendieck, if you want to use a string as an argument in your summary function, instead of embracing the argument with doubled braces ({{), you should use the .data pronoun as described in the Programming vignette: Loop over multiple variables:

mytable <- function( x, group ) {
  x %>% 
    group_by( .data[[group]] ) %>% 
    summarise( n = n() )
}

group_string <- 'Species'

mytable( iris, group_string )

`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
  Species        n
  <fct>      <int>
1 setosa        50
2 versicolor    50
3 virginica     50
1
  • Thanks! This is useful when using pmap.
    – Ernesto561
    Commented Jun 1, 2023 at 19:16
2

Ugly as they come, but she works:

mytable3 <- function(x, key) {
  my.call <- bquote(summarise(group_by(.(substitute(x)), NULL), n = n()))
  my.call[[2]][[3]] <- as.name(key)
  eval(my.call, parent.frame())
} 
mytable3(iris, "Species")
# Source: local data frame [3 x 2]
#
#      Species  n
# 1  virginica 50
# 2 versicolor 50
# 3     setosa 50

There are almost certainly cases that will cause this to break, but you get the idea. I don't think you can get around messing with the call. One other thing that did work but was even uglier is:

mytable4 <- function(x, key) summarise(group_by(x, x[[key]]), n = n())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.