我需要在表格的两列上运行简单的配对 t 检验(见下图),并且我希望它位于总摘要行(相关性下方)。
这是一个最小的工作示例供您使用:
library(tidyverse)
library(gt)
library(rstatix)
df <- structure(list(study = c("caius", "titium", "sempronius", "cicero",
"titus"), control = c(452, 603, 243, 552, 173), treatment = c(482,
383, 579, 360, 227), difference = c(30, -220, 336, -192, 54)), class = "data.frame", row.names = c(NA,
-5L))
df %>% gt() %>%
grand_summary_rows(
columns = c(control, treatment, difference),
fns = list(Mean ~ round(mean(.)),
SD ~ round(sd(.))),
missing_text = " "
) %>%
grand_summary_rows(
columns = c(difference),
fns = list(Correlation ~ round(cor(control, treatment), 2)),
missing_text = " "
)
我想在 gt 中运行测试可能会很复杂,所以我想将结果作为字符串添加到总摘要行中,有什么方法可以硬编码吗?
这取决于您想要为 t 检验显示哪些统计数据,但由于您的表已经包含两个变量的差异,您可以例如将 p 值添加到总摘要行,如下所示:
df %>%
gt() %>%
grand_summary_rows(
columns = c(control, treatment, difference),
fns = list(
Mean ~ round(mean(.)),
SD ~ round(sd(.))
),
missing_text = " "
) %>%
grand_summary_rows(
columns = c(difference),
fns = list(
Correlation ~ round(cor(control, treatment), 2),
"p-value" ~ t.test(.x)[["p.value"]]
),
missing_text = ""
)