【问题标题】:Keep other columns when doing group_by + summarise with dplyr使用 dplyr 进行 group_by + 汇总时保留其他列
【发布时间】:2022-07-21 05:01:37
【问题描述】:

我想只对具有一个组属性的两列执行group_by + summarise 操作,同时保持其他三列不变,每行具有相同的数字。我怎样才能做到这一点?例如

> data<- data.frame(a=1:10, b=rep(1,10), c=rep(2,10), d=rep(3,10), e= c("small", "med", "larg", "larg", "larg", "med", "small", "small", "small", "med"))
> data %>% group_by(e) %>% summarise(a=mean(a))
# A tibble: 3 × 2
  e         a
  <chr> <dbl>
1 larg   4   
2 med    6   
3 small  6.25

但我想要

# A tibble: 3 × 5
  e         a b     c     d
  <chr> <dbl> <dbl> <dbl> <dbl>
1 larg   4    1     2     3
2 med    6    1     2     3
3 small  6.25 1     2     3

group_by + summarise 总是删除其他列。我该怎么做?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    将其他列添加到group_by

    > library(tidyverse)
    > data <- data.frame(a=1:10, b=rep(1,10), c=rep(2,10), d=rep(3,10), e= c("small", "med", "larg", "larg", "larg", "med", "small", "small", "small", "med"))
    > data %>% group_by(e, b, c, d) %>% summarise(a=mean(a))
    `summarise()` has grouped output by 'e', 'b', 'c'. You can override using the `.groups` argument.
    # A tibble: 3 x 5
    # Groups:   e, b, c [3]
      e         b     c     d     a
      <chr> <dbl> <dbl> <dbl> <dbl>
    1 larg      1     2     3  4   
    2 med       1     2     3  6   
    3 small     1     2     3  6.25
    

    【讨论】:

      【解决方案2】:

      您始终可以使用group + summarise 计算一个新变量,并在摘要中添加across() 以保持数据框的其余部分“完整”。如果您的其他变量并不总是相同,这可能很有用。

      data %>% group_by(e) %>% 
          summarise(a=mean(a), across())
      
          # A tibble: 10 x 5
      # Groups:   e [3]
         e         a     b     c     d
         <chr> <dbl> <dbl> <dbl> <dbl>
       1 larg   4        1     2     3
       2 larg   4        1     2     3
       3 larg   4        1     2     3
       4 med    6        1     2     3
       5 med    6        1     2     3
       6 med    6        1     2     3
       7 small  6.25     1     2     3
       8 small  6.25     1     2     3
       9 small  6.25     1     2     3
      10 small  6.25     1     2     3
      

      【讨论】:

      • 这不是我想要的,你要创建这些重复的行
      【解决方案3】:

      尚不清楚您希望将多少列视为分组变量。如果数量很少,@tauft 的回答就足够了。否则,我们可以使用acrossgroup_by,这样我们就可以使用&lt;tidy-select&gt; 来选择要分组的列。

      library(dplyr)
      
      data2 <- data %>%
        group_by(across(-a)) %>%
        summarise(a = mean(a), .groups = "drop") %>%
        relocate(e, a, .before = b)
      data2
      # # A tibble: 3 x 5
      #   e         a     b     c     d
      #   <chr> <dbl> <dbl> <dbl> <dbl>
      # 1 larg   4        1     2     3
      # 2 med    6        1     2     3
      # 3 small  6.25     1     2     3
      

      上面也可以写成如下。

      data2 <- data %>%
        group_by(across(b:e)) %>%
        summarise(a = mean(a), .groups = "drop") %>%
        relocate(e, a, .before = b)
      

      【讨论】:

        猜你喜欢
        • 2016-12-29
        • 1970-01-01
        • 2020-07-10
        • 1970-01-01
        • 2020-05-15
        • 2020-06-19
        • 2014-06-17
        相关资源
        最近更新 更多