Sandro
Sandro

Reputation: 123

Add row totals to tbl_summary

I am attempting to add row totals to my tbl_summary().

Here is my code so far

ibrary(tidyverse)
library(gtsummary)
set.seed(42)
n <- 1000
dat <- data.frame(q=runif(n, min=45, max=85),
                  r=runif(n, min=2.4, max=6.0),
                  s=runif(n, min=24, max=60),
                  t=runif(n, min=0.28, max=1.73),
                  time=1)

patient <- data.frame(id=1:n,
                      treat = factor(sample(c('Treat','Control'), n, rep=TRUE, prob=c(.5, .5))),
                      age=sample(18:80, n, replace=TRUE),
                      sex = factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4))),
                      smoke=factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15))),
                      bmi=runif(n, min=16, max=45))
df <- cbind(patient, dat)

df %>% select(q, treat, smoke) %>% 
  tbl_continuous(variable = q, 
                 by = treat,
                 include = smoke) %>% add_n()

Is anyone able to assist with this last step. I think my issue is where to place the add_n() call. I have been using previously made gt_summary tables for guidance but something is not working this time.

Much appreciated everyone :)

Upvotes: 2

Views: 1039

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11784

There is not a straight-forward way unfortunately. But you can do it with the code below

ibrary(tidyverse)
#> Error in ibrary(tidyverse): could not find function "ibrary"
library(gtsummary)
set.seed(42)
n <- 1000
dat <- data.frame(q=runif(n, min=45, max=85),
                  r=runif(n, min=2.4, max=6.0),
                  s=runif(n, min=24, max=60),
                  t=runif(n, min=0.28, max=1.73),
                  time=1)

patient <- data.frame(id=1:n,
                      treat = factor(sample(c('Treat','Control'), n, rep=TRUE, prob=c(.5, .5))),
                      age=sample(18:80, n, replace=TRUE),
                      sex = factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4))),
                      smoke=factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15))),
                      bmi=runif(n, min=16, max=45))
df <- cbind(patient, dat)

tbl1 <- 
  df %>% 
  select(q, treat, smoke) %>% 
  tbl_continuous(
    variable = q,
    by = treat,
    include = smoke
  ) %>%
  bold_labels()

tbl_row_total <-
  df %>% 
  mutate(all_true = "Total") %>% 
  tbl_continuous(
    variable = q,
    by = treat,
    include = all_true
  ) %>%
  bold_levels() %>%
  modify_table_body(
    ~.x %>% dplyr::filter(row_type %in% "level")
  ) %>%
  modify_column_indent(columns = label, undo = TRUE) 

tbl_stack(list(tbl1, tbl_row_total)) %>%
  as_kable() # convert to kable to display on stackoverflow
Characteristic Control, N = 476 Treat, N = 524
smoke
Current 61 (53, 74) 64 (57, 73)
Former 66 (58, 76) 64 (53, 75)
Never 63 (54, 75) 65 (53, 74)
Total 64 (55, 75) 64 (54, 74)

Created on 2023-02-09 with reprex v2.0.2

Upvotes: 3

Related Questions