Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 461
itsMeInMiami Avatar asked Oct 18 '25 07:10

itsMeInMiami


1 Answers

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
like image 185
DJack Avatar answered Oct 20 '25 21:10

DJack