如何将R生成的重复测量ANOVA结果整理导出为Word格式表格
重复测量ANOVA结果导出Word表格实现方案
前置依赖
先安装加载所需工具包:
# 未安装依赖时先执行安装 install.packages(c("flextable", "officer", "dplyr", "tidyr", "purrr", "ez")) # 加载包 library(flextable) library(officer) library(dplyr) library(tidyr) library(purrr) library(ez)
运行你提供的基础代码生成统计结果数据集:
# 数据集构造 df <- tibble::tibble( sensors = c("S_A", "S_B", "S_C", "T_A", "T_B", "T_C"), readings = list( tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = 0, sd = 1) ), tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = 1, sd = 2) ), tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = -1, sd = 1.5) ), tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = 2, sd = 0.5) ), tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = 0.5, sd = 1.2) ), tibble::tibble( subj = factor(rep(sprintf("P%02d", 1:5), each = 3)), cohort = factor(rep("G0", 15)), session = factor(rep("X1", 15)), phase = factor(rep(c("A1", "A2", "A3"), times = 5)), measurement = rnorm(15, mean = -0.5, sd = 0.8) ) ) ) # ANOVA检验函数 fAddANOVA <- function(data) { data %>% ez::ezANOVA(dv = .(measurement), wid = .(subj), within = .(phase)) %>% as_tibble() } # 提取统计量 aov_stats <- df %>% dplyr::group_by(sensors) %>% dplyr::mutate(anova_output = purrr::map(readings, ~fAddANOVA(.x))) %>% dplyr::select(-readings) %>% tidyr::unnest(anova_output) # 清理冗余空值列 aov_clean <- aov_stats %>% select(-where(~all(is.na(.))))
方案1:拆分生成3个独立表格
# 构造三个子表 ## 表1:ANOVA核心测量指标 table1 <- aov_clean %>% select(sensors, Effect, DFn, DFd, SSn, SSd, F, `p`, `p<.05`) ## 表2:Mauchly球形检验结果 table2 <- aov_clean %>% select(sensors, Effect, Mauchly_W, Mauchly_p, `Mauchly_p<.05`) %>% filter(!is.na(Mauchly_W)) ## 表3:球形校正后检验结果 table3 <- aov_clean %>% select(sensors, Effect, GGe, `p[GG]`, `p[GG]<.05`, HFe, `p[HF]`, `p[HF]<.05`) %>% filter(!is.na(GGe)) # 批量导出到Word doc_split <- read_docx() doc_split <- doc_split %>% body_add_par("表1 重复测量ANOVA核心结果", style = "heading 2") %>% body_add_flextable(flextable(table1) %>% theme_booktabs() %>% autofit()) %>% body_add_break() %>% body_add_par("表2 Mauchly球形检验结果", style = "heading 2") %>% body_add_flextable(flextable(table2) %>% theme_booktabs() %>% autofit()) %>% body_add_break() %>% body_add_par("表3 球形校正后ANOVA结果", style = "heading 2") %>% body_add_flextable(flextable(table3) %>% theme_booktabs() %>% autofit()) # 保存文件 print(doc_split, target = "ANOVA拆分结果.docx")
方案2:合并生成带分层表头的统一表格
# 构造合并表,去除冗余列 table_union <- aov_clean %>% select( sensors, Effect, DFn, DFd, SSn, SSd, F, `p`, Mauchly_W, Mauchly_p, GGe, `p[GG]`, HFe, `p[HF]` ) # 生成带分层表头的格式化表格 ft_union <- flextable(table_union) %>% # 添加分组表头 add_header_row( top = TRUE, values = c("基础信息", "ANOVA核心结果", "Mauchly球形检验", "球形校正结果"), colwidths = c(2, 6, 2, 4) ) %>% # 格式美化 theme_booktabs() %>% bold(part = "header") %>% align(align = "center", part = "all") %>% autofit() # 导出到Word doc_union <- read_docx() doc_union <- doc_union %>% body_add_par("表 重复测量ANOVA完整检验结果", style = "heading 2") %>% body_add_flextable(ft_union) # 保存文件 print(doc_union, target = "ANOVA合并结果.docx")
可选优化
- 可通过flextable的
bg()函数将p值小于0.05的单元格标红,无需后续在Word中手动调整格式 - 若ezANOVA输出列名和示例不一致,直接调整
select()中的列名即可适配
内容的提问来源于stack exchange,提问作者12666727b9
相关产品推荐
相关产品推荐

