如何在数据帧的特定索引中插入行,其中仅在 R pipe dplyr 中包含上面几行的总和

How to insert rows in specific indices of dataframe containing sum of few rows above only in R pipe dplyr

对于下面的数据框,

df <- data.frame(id = c(rep(101, 4), rep(202, 3)),
                status = c("a","b","c","d", "a", "b", "c"),
                wt = c(100,200,100,105, 20,22,25),
                ht = c(5.3,5.2,5,5.1, 4.3,4.2,4.1))
    
df
   id status  wt  ht
1 101      a 100 5.3
2 101      b 200 5.2
3 101      c 100 5.0
4 101      d 105 5.1
5 202      a  20 4.3
6 202      b  22 4.2
7 202      c  25 4.1

我想获得以下输出:

> output
   id status  wt   ht
1 101      a 100  5.3
2 101      b 200  5.2
3 101      c 100  5.0
4 101      d 105  5.1
5 101    sum 505 20.6
6 202      a  20  4.3
7 202      b  22  4.2
8 202      c  25  4.1
9 202    sum  67 12.6

df 来自一系列管道,我不想停止它并做类似

的事情
output <- rbind(df[1:4,],
                c(101, "sum", colSums(df[1:4, c(3,4)])),
                df[5:7,],
                c(202, "sum", colSums(df[5:7, c(3,4)])))

我要找一个整洁、好看的!实现这一目标的方式。非常感谢任何帮助或想法。

df %>%
   ....

按'id'分组后,我们可能会使用adorn_totals

library(dplyr)
library(janitor)
df %>% 
  group_by(id) %>%
  group_modify(~ .x %>% 
      adorn_totals(cols = c(wt, ht), name = 'sum')) %>%
  ungroup

-输出

# A tibble: 9 × 4
     id status    wt    ht
  <dbl> <chr>  <dbl> <dbl>
1   101 a        100   5.3
2   101 b        200   5.2
3   101 c        100   5  
4   101 d        105   5.1
5   101 sum      505  20.6
6   202 a         20   4.3
7   202 b         22   4.2
8   202 c         25   4.1
9   202 sum       67  12.6

有了tibble::add_row,更灵活一点:

library(tidyverse)
df %>% 
  group_by(id) %>% 
  group_modify(~ .x %>% add_row(status = "sum", wt = sum(.$wt), ht = sum(.$ht)))

# A tibble: 9 × 4
# Groups:   id [2]
     id status    wt    ht
  <dbl> <chr>  <dbl> <dbl>
1   101 a        100   5.3
2   101 b        200   5.2
3   101 c        100   5  
4   101 d        105   5.1
5   101 sum      505  20.6
6   202 a         20   4.3
7   202 b         22   4.2
8   202 c         25   4.1
9   202 sum       67  12.6

另一种可能的解决方案,基于 colSumsdplyr

library(dplyr)
              
df %>% 
  group_by(id) %>% 
  group_modify(~ add_row(.x, status = "Sum", !!!colSums(.[2:3]))) %>% 
  ungroup

#> # A tibble: 9 × 4
#>      id status    wt    ht
#>   <dbl> <chr>  <dbl> <dbl>
#> 1   101 a        100   5.3
#> 2   101 b        200   5.2
#> 3   101 c        100   5  
#> 4   101 d        105   5.1
#> 5   101 Sum      505  20.6
#> 6   202 a         20   4.3
#> 7   202 b         22   4.2
#> 8   202 c         25   4.1
#> 9   202 Sum       67  12.6