使用Dplyr和Flextable计算行总和

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

Row sums with Dplyr and Flextable

问题

以下是翻译好的内容:

我有以下的数据框:

  1. 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创建了一个表格:

  1. data %>%
  2. pivot_wider(names_from=Annee, values_from=c(`Effectifs`)) %>%
  3. replace(is.na(.), 0) %>%
  4. arrange(match(Sexe, c("Filles", "Garçons"))) %>%
  5. arrange(match(Niveau, c("CP", "CE1"))) %>%
  6. flextable::flextable() %>%
  7. flextable::align(align = "center", part = "all") %>%
  8. theme_zebra()%>%
  9. merge_v(j = c("Niveau"))%>%
  10. border_outer(part = "all") %>%
  11. hline_top(part = "header") %>%
  12. hline(part="body")%>%
  13. align(align = "center", part = "header") %>%
  14. border_inner_v(part = "all")%>%
  15. valign(valign = "center", part = "header")%>%
  16. fix_border_issues(part = "all")

这给了我以下结果:

使用Dplyr和Flextable计算行总和

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

使用Dplyr和Flextable计算行总和

提前感谢您的帮助!

英文:

I have the following dataframe :

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

I made a table with flextable :

  1. data %>%
  2. pivot_wider(names_from=Annee, values_from=c(`Effectifs`)) %>%
  3. replace(is.na(.), 0) %>%
  4. arrange(match(Sexe, c("Filles", "Garçons"))) %>%
  5. arrange(match(Niveau, c("CP", "CE1"))) %>%
  6. flextable::flextable() %>%
  7. flextable::align(align = "center", part = "all") %>%
  8. theme_zebra()%>%
  9. merge_v(j = c("Niveau"))%>%
  10. border_outer(part = "all") %>%
  11. hline_top(part = "header") %>%
  12. hline(part="body")%>%
  13. align(align = "center", part = "header") %>%
  14. border_inner_v(part = "all")%>%
  15. valign(valign = "center", part = "header")%>%
  16. 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:

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

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. library(flextable)
  5. add_total <- function(x) {
  6. x %>%
  7. group_by(Niveau) %>%
  8. summarise(Sexe = "总计", across(where(is.numeric), sum)) %>%
  9. bind_rows(x)
  10. }
  11. data %>%
  12. pivot_wider(names_from = Annee, values_from = c(`Effectifs`)) %>%
  13. replace(is.na(.), 0) %>%
  14. add_total() %>%
  15. arrange(match(Sexe, c("女孩", "男孩", "总计"))) %>%
  16. arrange(match(Niveau, c("CP", "CE1"))) %>%
  17. flextable::flextable() %>%
  18. flextable::align(align = "center", part = "all") %>%
  19. theme_zebra() %>%
  20. merge_v(j = c("Niveau")) %>%
  21. border_outer(part = "all") %>%
  22. hline_top(part = "header") %>%
  23. hline(part = "body") %>%
  24. align(align = "center", part = "header") %>%
  25. border_inner_v(part = "all") %>%
  26. valign(valign = "center", part = "header") %>%
  27. fix_border_issues(part = "all") %>%
  28. bold(i = ~ Sexe == "总计")

使用Dplyr和Flextable计算行总和

英文:

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

  1. library(dplyr)
  2. library(tidyr)
  3. library(ggplot2)
  4. library(flextable)
  5. add_total &lt;- \(x) {
  6. x %&gt;%
  7. group_by(Niveau) %&gt;%
  8. summarise(Sexe = &quot;Total&quot;, across(where(is.numeric), sum)) %&gt;%
  9. bind_rows(x)
  10. }
  11. data %&gt;%
  12. pivot_wider(names_from = Annee, values_from = c(`Effectifs`)) %&gt;%
  13. replace(is.na(.), 0) %&gt;%
  14. add_total() %&gt;%
  15. arrange(match(Sexe, c(&quot;Filles&quot;, &quot;Gar&#231;ons&quot;, &quot;Total&quot;))) %&gt;%
  16. arrange(match(Niveau, c(&quot;CP&quot;, &quot;CE1&quot;))) %&gt;%
  17. flextable::flextable() %&gt;%
  18. flextable::align(align = &quot;center&quot;, part = &quot;all&quot;) %&gt;%
  19. theme_zebra() %&gt;%
  20. merge_v(j = c(&quot;Niveau&quot;)) %&gt;%
  21. border_outer(part = &quot;all&quot;) %&gt;%
  22. hline_top(part = &quot;header&quot;) %&gt;%
  23. hline(part = &quot;body&quot;) %&gt;%
  24. align(align = &quot;center&quot;, part = &quot;header&quot;) %&gt;%
  25. border_inner_v(part = &quot;all&quot;) %&gt;%
  26. valign(valign = &quot;center&quot;, part = &quot;header&quot;) %&gt;%
  27. fix_border_issues(part = &quot;all&quot;) %&gt;%
  28. 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:

确定