如何将 group_by 与 summarise 和 summarise_all 一起使用?

una*_*der 2 r dataframe dplyr tidyverse

   x  y
1  1  1
2  3  2
3  2  3
4  3  4
5  2  5
6  4  6
7  5  7
8  2  8
9  1  9
10 1 10
11 3 11
12 4 12
Run Code Online (Sandbox Code Playgroud)

以上是输入的一部分。

假设它还有很多其他列

我想要:

  1. 按 x 分组
  2. 通过总和总结 y
  3. 对于所有其他列,我想通过仅取第一个值来 summarise_all

Jon*_*ing 5

这是一种将其分解为两个问题并将它们结合起来的方法:

library(dplyr)
left_join(
  # Here we want to treat column y specially
  df %>%
    group_by(x) %>%
    summarize(sum_y = sum(y)),
  # Here we exclude y and use a different summation for all the remaining columns
  df %>%
    group_by(x) %>%
    select(-y) %>%
    summarise_all(first)
  ) 

# A tibble: 5 x 3
      x sum_y     z
  <int> <int> <int>
1     1    20     1
2     2    16     3
3     3    17     2
4     4    18     2
5     5     7     3
Run Code Online (Sandbox Code Playgroud)

样本数据:

df <- read.table(
  header = T, 
  stringsAsFactors = F,
  text="x  y z
        1  1 1
        3  2 2
        2  3 3
        3  4 4
        2  5 1
        4  6 2
        5  7 3
        2  8 4
        1  9 1
        1 10 2
        3 11 3
        4 12 4")
Run Code Online (Sandbox Code Playgroud)