如何使用 dplyr::across() 中的 n() 按组计算行数?
How to count rows by group with n() inside dplyr::across()?
在以前的 dplyr 版本中,如果我想使用 summarise()
获取行数以及其他汇总值,我可以执行类似
的操作
library(tidyverse)
df <- tibble(
group = c("A", "A", "B", "B", "C"),
value = c(1, 2, 3, 4, 5)
)
df %>%
group_by(group) %>%
summarise(total = sum(value), count = n())
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
group total count
<chr> <dbl> <int>
1 A 3 2
2 B 7 2
3 C 5 1
我使用新 across()
函数获得相同输出的直觉是
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum, count = n)))
Error: Problem with `summarise()` input `..1`.
x unused argument (col)
ℹ Input `..1` is `across(value, list(sum = sum, count = n))`.
ℹ The error occurred in group 1: group = "A".
此问题特定于 n()
函数,只需调用 sum()
即可正常工作:
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum)))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
group value_sum
<chr> <dbl>
1 A 3
2 B 7
3 C 5
我尝试了各种语法变体(使用 lambda、试验 cur_group()
等),但都无济于事。如何在 across()
内获得所需的结果?
我们可以对 n()
使用 lambda 函数,而 sum
可以通过调用它来调用,如果没有其他要指定的参数
library(dplyr)
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum, count = ~ n())), .groups = 'drop')
-输出
# A tibble: 3 x 3
# group value_sum value_count
# <chr> <dbl> <int>
#1 A 3 2
#2 B 7 2
#3 C 5 1
在以前的 dplyr 版本中,如果我想使用 summarise()
获取行数以及其他汇总值,我可以执行类似
library(tidyverse)
df <- tibble(
group = c("A", "A", "B", "B", "C"),
value = c(1, 2, 3, 4, 5)
)
df %>%
group_by(group) %>%
summarise(total = sum(value), count = n())
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 3
group total count
<chr> <dbl> <int>
1 A 3 2
2 B 7 2
3 C 5 1
我使用新 across()
函数获得相同输出的直觉是
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum, count = n)))
Error: Problem with `summarise()` input `..1`.
x unused argument (col)
ℹ Input `..1` is `across(value, list(sum = sum, count = n))`.
ℹ The error occurred in group 1: group = "A".
此问题特定于 n()
函数,只需调用 sum()
即可正常工作:
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum)))
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
group value_sum
<chr> <dbl>
1 A 3
2 B 7
3 C 5
我尝试了各种语法变体(使用 lambda、试验 cur_group()
等),但都无济于事。如何在 across()
内获得所需的结果?
我们可以对 n()
使用 lambda 函数,而 sum
可以通过调用它来调用,如果没有其他要指定的参数
library(dplyr)
df %>%
group_by(group) %>%
summarise(across(value, list(sum = sum, count = ~ n())), .groups = 'drop')
-输出
# A tibble: 3 x 3
# group value_sum value_count
# <chr> <dbl> <int>
#1 A 3 2
#2 B 7 2
#3 C 5 1