R:如何在每一行下面按年份汇总

fumotvh3  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(84)

我得到了下面的数据框和总结我的数据框的函数

library(dplyr)
library(tidyr)
df_so = data.frame(Ano=c(2017, 2017, 2017, 2018, 2018, 2018, 2019, 2019), 
                   Metodo=c("x","y","x","x","x","x","z","x"))
df_so %>%
  group_by(Ano, Metodo) %>%
  tally()%>%
  rename(quantidade = n)%>%
  summarise(Ano = c("", Ano), Metodo = c('Total', Metodo),  
            across(quantidade, ~c(sum(., na.rm = TRUE), .)))

所以我得到了结果:

Ano    Metodo quantidade
  <chr>  <chr>       <int>
1 ""     Total           3
2 "2017" x               2
3 "2017" y               1
4 ""     Total           3
5 "2018" x               3
6 ""     Total           2
7 "2019" x               1
8 "2019" z               1

但是,我想:

Ano    Metodo quantidade
  <chr>  <chr>       <int>
1 "2017" x               2
2 "2017" y               1
3 "2017" Total           3
4 "2018" x               3
5 "2018" Total           3
6 "2019" x               1
7 "2019" z               1  
8 "2019" Total           2

我怎么能得到这样的结果呢?

daolsyd0

daolsyd01#

tally之后,可以使用group_modifybind_rows为每个组添加额外的summarise行。
由于在所需的输出中有一个字符Ano列,因此我在最后添加了一个额外的mutate来实现这一点。

library(dplyr)

df_so %>% 
  group_by(Ano, Metodo) %>% 
  tally() %>% 
  group_modify(~bind_rows(.x, summarize(.x, Metodo = "Total", n = sum(n)))) %>% 
  ungroup() %>% 
  mutate(Ano = as.character(Ano))

# A tibble: 8 × 3
  Ano   Metodo     n
  <chr> <chr>  <int>
1 2017  x          2
2 2017  y          1
3 2017  Total      3
4 2018  x          3
5 2018  Total      3
6 2019  x          1
7 2019  z          1
8 2019  Total      2
sulc1iza

sulc1iza2#

**1)**我们可以使用janitor包中的adorn_totals

library(dplyr)
library(janitor)

df_so %>%
  count(Ano, Metodo, name = "quant") %>%
  reframe(adorn_totals(across()), .by = Ano)

给出:

# A tibble: 8 x 3
    Ano Metodo quant
  <dbl> <chr>  <int>
1  2017 x          2
2  2017 y          1
3  2017 Total      3
4  2018 x          3
5  2018 Total      3
6  2019 x          1
7  2019 z          1
8  2019 Total      2

**2)**可以使用table制作简单的二维布局。不使用封装。

addmargins(table(df_so))

给出:

Metodo
Ano    x y z Sum
  2017 2 1 0   3
  2018 3 0 0   3
  2019 1 0 1   2
  Sum  6 1 1   8

或者等同于这个管道

df_so |> table() |> addmargins()

或长形式:

ftable(addmargins(table(df_so)), row.vars = 1:2)

给出:

Ano  Metodo   
2017 x       2
     y       1
     z       0
     Sum     3
2018 x       3
     y       0
     z       0
     Sum     3
2019 x       1
     y       0
     z       1
     Sum     2
Sum  x       6
     y       1
     z       1
     Sum     8

或者等同于这个管道

df_so |> table() |> addmargins() |> ftable(row.vars = 1:2)

相关问题