使用Dplyr和Flextable计算行总和

huangapple go评论67阅读模式
英文:

Row sums with Dplyr and Flextable

问题

以下是翻译好的内容:

我有以下的数据框:

structure(list(Annee = c("2021-2022 ", "2021-2022 ", "2021-2022 ", "2022-2023 ", "2022-2023 ", "2022-2023 ", "2022-2023 "), Niveau = c("CE1", "CP", "CP", "CE1", "CE1", "CP", "CP"), Sexe = c("Garçons", "Filles", "Garçons", "Filles", "Garçons", "Filles", "Garçons"), Effectifs = c(4, 1, 2, 1, 2, 4, 3)), row.names = c(NA, -7L), class = "data.frame")

我用flextable创建了一个表格:

data %>%
  pivot_wider(names_from=Annee, values_from=c(`Effectifs`)) %>%
  replace(is.na(.), 0) %>%
  arrange(match(Sexe, c("Filles", "Garçons"))) %>%
  arrange(match(Niveau, c("CP", "CE1"))) %>%
  flextable::flextable() %>%
  flextable::align(align = "center", part = "all") %>%
  theme_zebra()%>%
  merge_v(j = c("Niveau"))%>%
  border_outer(part = "all") %>%
  hline_top(part = "header") %>%
  hline(part="body")%>%
  align(align = "center", part = "header") %>% 
  border_inner_v(part = "all")%>%
  valign(valign = "center", part = "header")%>%
  fix_border_issues(part = "all")

这给了我以下结果:

使用Dplyr和Flextable计算行总和

我需要在flextable中添加一行,该行是每个“Niveau”的“Effectifs”的总和。以下是预期结果:

使用Dplyr和Flextable计算行总和

提前感谢您的帮助!

英文:

I have the following dataframe :

structure(list(Annee = c("2021-2022 ", "2021-2022 ", "2021-2022 ", 
"2022-2023 ", "2022-2023 ", "2022-2023 ", "2022-2023 "), Niveau = c("CE1", 
"CP", "CP", "CE1", "CE1", "CP", "CP"), Sexe = c("Garçons", "Filles", 
"Garçons", "Filles", "Garçons", "Filles", "Garçons"), Effectifs = c(4, 
1, 2, 1, 2, 4, 3)), row.names = c(NA, -7L), class = "data.frame")

I made a table with flextable :

data %>%
  pivot_wider(names_from=Annee, values_from=c(`Effectifs`)) %>%
  replace(is.na(.), 0) %>%
  arrange(match(Sexe, c("Filles", "Garçons"))) %>%
  arrange(match(Niveau, c("CP", "CE1"))) %>%
  flextable::flextable() %>% 
  flextable::align(align = "center", part = "all") %>%
  theme_zebra()%>%
  merge_v(j = c("Niveau"))%>%
  border_outer(part = "all") %>%
  hline_top(part = "header") %>%
  hline(part="body")%>%
  align(align = "center", part = "header") %>% 
  border_inner_v(part = "all")%>% 
  valign(valign = "center", part = "header")%>% 
  fix_border_issues(part = "all")

Which give me this result :
使用Dplyr和Flextable计算行总和

I need to add a new row in the flextable which would be the sum of "Effectifs" for each "Niveau". Here's the expected result :
使用Dplyr和Flextable计算行总和

Thank you in advance for your help!

答案1

得分: 2

Here is the translated content:

使用一个小的自定义函数来为您的数据添加总计行,您可以执行以下操作:

library(dplyr)
library(tidyr)
library(ggplot2)
library(flextable)

add_total <- function(x) {
  x %>%
    group_by(Niveau) %>%
    summarise(Sexe = "总计", across(where(is.numeric), sum)) %>%
    bind_rows(x)
}

data %>%
  pivot_wider(names_from = Annee, values_from = c(`Effectifs`)) %>%
  replace(is.na(.), 0) %>%
  add_total() %>%
  arrange(match(Sexe, c("女孩", "男孩", "总计"))) %>%
  arrange(match(Niveau, c("CP", "CE1"))) %>%
  flextable::flextable() %>%
  flextable::align(align = "center", part = "all") %>%
  theme_zebra() %>%
  merge_v(j = c("Niveau")) %>%
  border_outer(part = "all") %>%
  hline_top(part = "header") %>%
  hline(part = "body") %>%
  align(align = "center", part = "header") %>%
  border_inner_v(part = "all") %>%
  valign(valign = "center", part = "header") %>%
  fix_border_issues(part = "all") %>%
  bold(i = ~ Sexe == "总计")

使用Dplyr和Flextable计算行总和

英文:

Using a small custom function to add the totals row to your data you could do:

library(dplyr)
library(tidyr)
library(ggplot2)
library(flextable)

add_total &lt;- \(x) {
  x %&gt;%
    group_by(Niveau) %&gt;%
    summarise(Sexe = &quot;Total&quot;, across(where(is.numeric), sum)) %&gt;%
    bind_rows(x)
}

data %&gt;%
  pivot_wider(names_from = Annee, values_from = c(`Effectifs`)) %&gt;%
  replace(is.na(.), 0) %&gt;%
  add_total() %&gt;%
  arrange(match(Sexe, c(&quot;Filles&quot;, &quot;Gar&#231;ons&quot;, &quot;Total&quot;))) %&gt;%
  arrange(match(Niveau, c(&quot;CP&quot;, &quot;CE1&quot;))) %&gt;%
  flextable::flextable() %&gt;%
  flextable::align(align = &quot;center&quot;, part = &quot;all&quot;) %&gt;%
  theme_zebra() %&gt;%
  merge_v(j = c(&quot;Niveau&quot;)) %&gt;%
  border_outer(part = &quot;all&quot;) %&gt;%
  hline_top(part = &quot;header&quot;) %&gt;%
  hline(part = &quot;body&quot;) %&gt;%
  align(align = &quot;center&quot;, part = &quot;header&quot;) %&gt;%
  border_inner_v(part = &quot;all&quot;) %&gt;%
  valign(valign = &quot;center&quot;, part = &quot;header&quot;) %&gt;%
  fix_border_issues(part = &quot;all&quot;) %&gt;%
  bold(i = ~ Sexe == &quot;Total&quot;)

使用Dplyr和Flextable计算行总和

huangapple
  • 本文由 发表于 2023年6月13日 15:12:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定