如何在函数中使用 dplyr::group_by
How to use dplyr::group_by in a function
我想创建一个函数来生成 table,该函数具有基于一个或多个分组变量的计数。我发现这个 post 如果我向函数传递一个变量名
就可以工作
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)
效果很好,但我不知道如何将两个变量传递给函数。
这类作品:
countString(paste(animal, sex))
它给了我正确的计数,但返回的 table 将动物和性别变量合并为一个变量。
# 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
向函数传递以逗号分隔的两个单词的语法是什么?我想得到这个结果:
# 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
您可以使用 group_by_at
和列索引,例如:
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
对于多个参数,我们将 'things' 替换为 ...
,类似地,对于多个参数,将 enquos
替换为 !!!
。用 count
删除了 group_by
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
我想创建一个函数来生成 table,该函数具有基于一个或多个分组变量的计数。我发现这个 post
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)
效果很好,但我不知道如何将两个变量传递给函数。 这类作品:
countString(paste(animal, sex))
它给了我正确的计数,但返回的 table 将动物和性别变量合并为一个变量。
# 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
向函数传递以逗号分隔的两个单词的语法是什么?我想得到这个结果:
# 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
您可以使用 group_by_at
和列索引,例如:
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
对于多个参数,我们将 'things' 替换为 ...
,类似地,对于多个参数,将 enquos
替换为 !!!
。用 count
group_by
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