gt_summary tabulating mean/average counts with p-values

Hello there,

I'm trying to tabulate the mean or average counts daily (count/number_days), stratified by period and category, and calculate the p-value using the following data frame:

library(gtsummary)

df <-
  data.frame(
    number_days= c(120,  80, 120, 80), #number of days within the 2 period groups
    count = c(1000, 400, 700, 600),
    period = c("A", "B", "A", "B"),
    category=c("Relevant", "Relevant", "Other", "Other"))

I run the script below, using tbl_summary, to determine mean count, period and category:

df %>%
  select(count, period) %>%
  tbl_summary(by = period,
              statistic = list(all_continuous() ~ "{mean} ({sd})",
              all_categorical() ~ "{n} / {N} ({p}%)"),
              digits = all_continuous() ~ 2,
              type = list(count ~ "continuous")) %>%
  add_p()

However, this doesn't work to produce what I'm intending to create, which is a 2 by 2 table with average counts and a p-value that tests for statistical significance:

image

I also have the row-level data (with with an exact date variable), not just the data frame with the aggregate data. How can I go about producing what I need to produce?

Any help would be appreciated! Thanks in advance!

Hi @R_Inquirer,
This works OK

library(dplyr)
library(gt)
library(gtsummary)

df <-
  data.frame(
    number_days= c(120,  80, 120, 80), #number of days within the 2 period groups
    count = c(1000, 400, 700, 600),
    period = c("A", "B", "A", "B"),
    category=c("Relevant", "Relevant", "Other", "Other"))

df

df %>%
  #select(count, period) %>%
  select(-category) %>%
  tbl_summary(by = period,
              statistic = list(all_continuous() ~ "{mean} ({sd})",
                               all_categorical() ~ "{n} / {N} ({p}%)"),
              digits = all_continuous() ~ 2,
              type = list(count ~ "continuous")) %>%
  add_p()

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.