6

I would to split the values of N and % into two separate columns ie, N and % columns

library(gtsummary)

trial %>% 
  select(response, grade) %>% 
  tbl_summary()
Magut
  • 151
  • 7

1 Answers1

2

The easiest way to do this is with tbl_merge(), constructing two tables (one with N the other with %). Example below!

library(gtsummary)
#> #BlackLivesMatter
library(tidyverse)


tbl <- 
  # iterate over these two statistics
  c("{n} / {N}", "{p}%") %>%
  # build tbl_summary using each of the stats
  map(
    ~trial %>% 
      select(response, grade) %>% 
      tbl_summary(
        statistic = all_categorical() ~ .x,
        missing = "no"
      ) 
  ) %>%
  # merge the two tables together
  tbl_merge() %>%
  # some formatting to make it cute
  modify_spanning_header(everything() ~ NA) %>%
  modify_footnote(everything() ~ NA) %>%
  modify_header(list(stat_0_1 ~ "**n / N**", stat_0_2 ~ "**percent**"))

enter image description here Created on 2021-08-06 by the reprex package (v2.0.0)

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28