英文:
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")
这给了我以下结果:
我需要在flextable中添加一行,该行是每个“Niveau”的“Effectifs”的总和。以下是预期结果:
提前感谢您的帮助!
英文:
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")
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 :
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 == "总计")
英文:
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 <- \(x) {
x %>%
group_by(Niveau) %>%
summarise(Sexe = "Total", 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("Filles", "Garçons", "Total"))) %>%
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 == "Total")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论