[英]Maintain original column titles with dplyr function in loop R
Let's say I have a dataset like mtcars and I would like to loop over different columns to calculate counts (among others).假设我有一个像 mtcars 这样的数据集,我想遍历不同的列来计算计数(以及其他)。
library(dplyr)
df <- mtcars
groups <- c('cyl', 'hp')
for(g in groups) {
group_counts <- df %>%
group_by(get(g)) %>%
count()
print(group_counts)
}
Which gives me the following for the cyl column and something similar for the hp column:这为我提供了 cyl 列的以下内容和 hp 列的类似内容:
`get(g)` n
4 11
6 7
8 14
How do I manage to the first column to return the original column name instead of the 'get(g)'?我如何管理到第一列以返回原始列名而不是“get(g)”? Like this
像这样
cyl n
4 11
6 7
8 14
You can use across(all_of())
like this:您可以像这样使用
across(all_of())
:
for(g in groups) {
group_counts <- df %>%
group_by(across(all_of(g))) %>%
count()
print(group_counts)
}
Output: Output:
# A tibble: 3 × 2
# Groups: cyl [3]
cyl n
<dbl> <int>
1 4 11
2 6 7
3 8 14
# A tibble: 22 × 2
# Groups: hp [22]
hp n
<dbl> <int>
1 52 1
2 62 1
3 65 1
4 66 2
5 91 1
6 93 1
7 95 1
8 97 1
9 105 1
10 109 1
# … with 12 more rows
groups
is a character vector. groups
是一个特征向量。 To reference objects with corresponding names, we can convert to sym
bol and evaluate !!
要引用具有相应名称的对象,我们可以转换为
sym
并计算!!
: :
for(g in groups) {
group_counts <- df %>%
group_by(!!sym(g)) %>%
count()
print(group_counts)
}
# A tibble: 3 × 2
# Groups: cyl [3]
cyl n
<dbl> <int>
1 4 11
2 6 7
3 8 14
# A tibble: 22 × 2
# Groups: hp [22]
hp n
<dbl> <int>
1 52 1
2 62 1
3 65 1
4 66 2
5 91 1
6 93 1
7 95 1
8 97 1
9 105 1
10 109 1
# … with 12 more rows
# ℹ Use `print(n = ...)` to see more rows
We can also convert groups
into a vector of symbols outside the loop, and evaluate !!
我们还可以将
groups
转换为循环外的符号向量,并计算!!
inside the loop:在循环内:
my_function <- function(df, groups) {
groups <- syms(groups)
for(g in groups) {
group_counts <- df %>%
group_by(!!g) %>%
count()
print(group_counts)
}
}
my_function(df, groups)
# A tibble: 3 × 2
# Groups: cyl [3]
cyl n
<dbl> <int>
1 4 11
2 6 7
3 8 14
# A tibble: 22 × 2
# Groups: hp [22]
hp n
<dbl> <int>
1 52 1
2 62 1
3 65 1
4 66 2
5 91 1
6 93 1
7 95 1
8 97 1
9 105 1
10 109 1
# … with 12 more rows
# ℹ Use `print(n = ...)` to see more rows
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.