在汇总行添加统计结果

问题描述 投票:0回答:1

我需要在表格的两列上运行简单的配对 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 中运行测试可能会很复杂,所以我想将结果作为字符串添加到总摘要行中,有什么方法可以硬编码吗?

r r-markdown quarto gt
1个回答
0
投票

这取决于您想要为 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 = ""
  )

© www.soinside.com 2019 - 2024. All rights reserved.