itsMeInMiami
itsMeInMiami

Reputation: 2773

How to use dplyr::group_by in a function

I would like to create a function that will produce a table that has counts based on one or more grouping variables. I found this post Using dplyr group_by in a function which works if I pass the function a single variable name

library(dplyr)
l <- c("a", "b", "c", "e", "f", "g")
animal <- c("dog", "cat", "dog", "dog", "cat", "fish")
sex <- c("m", "f", "f", "m", "f", "unknown")
n <- rep(1, length(animal))
theTibble <- tibble(l, animal, sex, n)


countString <- function(things) {
  theTibble %>% group_by(!! enquo(things)) %>% count()
}

countString(animal)
countString(sex)

That works nicely but I don't know how to pass the function two variables. This sort of works:

countString(paste(animal, sex))

It gives me the correct counts but the returned table collapses the animal and sex variables into one variable.

# A tibble: 4 x 2
# Groups:   paste(animal, sex) [4]
  `paste(animal, sex)`    nn
  <chr>                <int>
1 cat f                    2
2 dog f                    1
3 dog m                    2
4 fish unknown             1

What is the syntax for passing a function two words separated by commas? I want to get this result:

# A tibble: 4 x 3
# Groups:   animal, sex [4]
  animal sex        nn
  <chr>  <chr>   <int>
1 cat    f           2
2 dog    f           1
3 dog    m           2
4 fish   unknown     1

Upvotes: 4

Views: 794

Answers (2)

akrun
akrun

Reputation: 887851

We replaced 'things' with ... for multiple arguments, similarly enquos with !!! for multiple arguments. Removed the group_by with count

countString <- function(...) {
  grps <- enquos(...)
  theTibble %>%
       count(!!! grps) 
}


countString(sex)
# A tibble: 3 x 2
#  sex        nn
#  <chr>   <int>
#1 f           3
#2 m           2
#3 unknown     1

countString(animal)
# A tibble: 3 x 2
#  animal    nn
#  <chr>  <int>
#1 cat        2
#2 dog        3
#3 fish       1

countString(animal, sex)
# A tibble: 4 x 3
#  animal sex        nn
#  <chr>  <chr>   <int>
#1 cat    f           2
#2 dog    f           1
#3 dog    m           2
#4 fish   unknown     1

Upvotes: 3

DJack
DJack

Reputation: 4940

You can use group_by_at and column index such as:

countString <- function(things) {
  index <- which(colnames(theTibble) %in% things)
  theTibble %>% 
       group_by_at(index) %>% 
       count()
}

countString(c("animal", "sex"))

## A tibble: 4 x 3
## Groups:   animal, sex [4]
#  animal sex        nn
#  <chr>  <chr>   <int>
#1 cat    f           2
#2 dog    f           1
#3 dog    m           2
#4 fish   unknown     1

Upvotes: 5

Related Questions