如何使用 dplyr::across() 内的 n() 按组计算行数?

nic*_*low 7 r dplyr across

在以前版本的 dplyr 中,如果我想使用 获取行计数以及其他汇总值summarise(),我可以执行类似的操作

\n
library(tidyverse)\n\ndf <- tibble(\n    group = c("A", "A", "B", "B", "C"),\n    value = c(1, 2, 3, 4, 5)\n)\n\ndf %>%\n    group_by(group) %>% \n    summarise(total = sum(value), count = n())\n\n`summarise()` ungrouping output (override with `.groups` argument)\n\n# A tibble: 3 x 3\n  group total count\n  <chr> <dbl> <int>\n1 A         3     2\n2 B         7     2\n3 C         5     1\n
Run Code Online (Sandbox Code Playgroud)\n

我使用新函数获得相同输出的本能across()

\n
df %>%\n  group_by(group) %>% \n  summarise(across(value, list(sum = sum, count = n)))\nError: Problem with `summarise()` input `..1`.\nx unused argument (col)\n\xe2\x84\xb9 Input `..1` is `across(value, list(sum = sum, count = n))`.\n\xe2\x84\xb9 The error occurred in group 1: group = "A".\n
Run Code Online (Sandbox Code Playgroud)\n

该问题特定于该n()函数,只需调用即可按sum()预期工作:

\n
df %>%\n  group_by(group) %>% \n  summarise(across(value, list(sum = sum)))\n`summarise()` ungrouping output (override with `.groups` argument)\n# A tibble: 3 x 2\n  group value_sum\n  <chr>     <dbl>\n1 A             3\n2 B             7\n3 C             5\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试了各种语法变体(使用 lambda、尝试cur_group()等),但无济于事。我怎样才能得到想要的结果across()

\n

akr*_*run 11

我们可以使用 lamdba 函数,而n()如果sum没有指定其他参数,则可以通过调用它来调用它

library(dplyr)
df %>%
  group_by(group) %>% 
  summarise(across(value, list(sum = sum, count = ~ n())), .groups = 'drop')
Run Code Online (Sandbox Code Playgroud)

-输出

# 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
Run Code Online (Sandbox Code Playgroud)

  • 就这样,谢谢阿克伦。我之前曾尝试过``~n(.x)````,但我没有想到要尝试不带参数的 lambda。 (2认同)