Summary table for all the variables

Code
library(gtsummary)
data(trial)
tbl_summary(trial)
Characteristic N = 2001
Chemotherapy Treatment
    Drug A 98 (49%)
    Drug B 102 (51%)
Age 47 (38, 57)
    Unknown 11
Marker Level (ng/mL) 0.64 (0.22, 1.39)
    Unknown 10
T Stage
    T1 53 (27%)
    T2 54 (27%)
    T3 43 (22%)
    T4 50 (25%)
Grade
    I 68 (34%)
    II 68 (34%)
    III 64 (32%)
Tumor Response 61 (32%)
    Unknown 7
Patient Died 112 (56%)
Months to Death/Censor 22.4 (16.0, 24.0)
1 n (%); Median (IQR)

Summary table for some variables

Code
trial %>% 
  select(c("trt", "age", "stage")) %>% 
  tbl_summary()
Characteristic N = 2001
Chemotherapy Treatment
    Drug A 98 (49%)
    Drug B 102 (51%)
Age 47 (38, 57)
    Unknown 11
T Stage
    T1 53 (27%)
    T2 54 (27%)
    T3 43 (22%)
    T4 50 (25%)
1 n (%); Median (IQR)

Summary table by treatment

Code
tbl_summary(trial, by=trt)
Characteristic Drug A, N = 981 Drug B, N = 1021
Age 46 (37, 59) 48 (39, 56)
    Unknown 7 4
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20)
    Unknown 6 4
T Stage

    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
    Unknown 3 4
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0)
1 Median (IQR); n (%)
Code
trial %>% 
  tbl_summary(by=trt)
Characteristic Drug A, N = 981 Drug B, N = 1021
Age 46 (37, 59) 48 (39, 56)
    Unknown 7 4
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20)
    Unknown 6 4
T Stage

    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
    Unknown 3 4
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0)
1 Median (IQR); n (%)

Summary table by treatment with no missings line

Code
tbl_summary(trial, by=trt, missing = "no")
Characteristic Drug A, N = 981 Drug B, N = 1021
Age 46 (37, 59) 48 (39, 56)
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20)
T Stage

    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0)
1 Median (IQR); n (%)

Summary table by treatment adding p-value

Code
trial %>% 
  tbl_summary(by=trt, missing = "no") %>%  
  add_p()
Characteristic Drug A, N = 981 Drug B, N = 1021 p-value2
Age 46 (37, 59) 48 (39, 56) 0.7
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.085
T Stage

0.9
    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

0.9
    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%) 0.5
Patient Died 52 (53%) 60 (59%) 0.4
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0) 0.14
1 Median (IQR); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test

Summary table by treatment adding p-value and N totals

Code
trial %>% 
  tbl_summary(by=trt, missing = "no") %>%  
  add_p() %>% 
  add_n()
Characteristic N Drug A, N = 981 Drug B, N = 1021 p-value2
Age 189 46 (37, 59) 48 (39, 56) 0.7
Marker Level (ng/mL) 190 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.085
T Stage 200

0.9
    T1
28 (29%) 25 (25%)
    T2
25 (26%) 29 (28%)
    T3
22 (22%) 21 (21%)
    T4
23 (23%) 27 (26%)
Grade 200

0.9
    I
35 (36%) 33 (32%)
    II
32 (33%) 36 (35%)
    III
31 (32%) 33 (32%)
Tumor Response 193 28 (29%) 33 (34%) 0.5
Patient Died 200 52 (53%) 60 (59%) 0.4
Months to Death/Censor 200 23.5 (17.4, 24.0) 21.2 (14.6, 24.0) 0.14
1 Median (IQR); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test

Summary table by treatment adding p-value with 3 decimal places

Code
trial  %>% 
  tbl_summary(by = trt, missing="no") %>%  
  add_p(pvalue_fun = ~style_sigfig(.,digits=3)) %>% add_n()
Characteristic N Drug A, N = 981 Drug B, N = 1021 p-value2
Age 189 46 (37, 59) 48 (39, 56) 0.718
Marker Level (ng/mL) 190 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.085
T Stage 200

0.866
    T1
28 (29%) 25 (25%)
    T2
25 (26%) 29 (28%)
    T3
22 (22%) 21 (21%)
    T4
23 (23%) 27 (26%)
Grade 200

0.871
    I
35 (36%) 33 (32%)
    II
32 (33%) 36 (35%)
    III
31 (32%) 33 (32%)
Tumor Response 193 28 (29%) 33 (34%) 0.530
Patient Died 200 52 (53%) 60 (59%) 0.412
Months to Death/Censor 200 23.5 (17.4, 24.0) 21.2 (14.6, 24.0) 0.145
1 Median (IQR); n (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test

Summary table by treatment with specific summary statistics for some variables and specific test

Code
trial  %>% 
  tbl_summary(by = trt, missing="no", statistic = list("age" ~ "{mean} ({sd})")) %>%  
  add_p(test = list(c("age") ~ 't.test')) 
Characteristic Drug A, N = 981 Drug B, N = 1021 p-value2
Age 47 (15) 47 (14) 0.8
Marker Level (ng/mL) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.085
T Stage

0.9
    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

0.9
    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%) 0.5
Patient Died 52 (53%) 60 (59%) 0.4
Months to Death/Censor 23.5 (17.4, 24.0) 21.2 (14.6, 24.0) 0.14
1 Mean (SD); Median (IQR); n (%)
2 Welch Two Sample t-test; Wilcoxon rank sum test; Pearson’s Chi-squared test
Code
#with labels for the statistics and specific tests
trial  %>% 
  tbl_summary(by = trt, 
  statistic = list("age" ~ "{mean} ({sd})"), missing="no") %>%  
  add_p(test = list(c("age") ~ 't.test')) %>% 
  separate_p_footnotes() %>% 
  add_stat_label() 
Characteristic Drug A, N = 98 Drug B, N = 102 p-value
Age, Mean (SD) 47 (15) 47 (14) 0.81
Marker Level (ng/mL), Median (IQR) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20) 0.0852
T Stage, n (%)

0.93
    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade, n (%)

0.93
    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response, n (%) 28 (29%) 33 (34%) 0.53
Patient Died, n (%) 52 (53%) 60 (59%) 0.43
Months to Death/Censor, Median (IQR) 23.5 (17.4, 24.0) 21.2 (14.6, 24.0) 0.142
1 Welch Two Sample t-test
2 Wilcoxon rank sum test
3 Pearson’s Chi-squared test

Summary table with some options for formating of headings and labels

Code
trial %>% 
  tbl_summary(by = trt) %>% 
  add_overall %>% 
  add_n %>% 
  modify_header(label ~ "**Variable**") %>% 
  modify_spanning_header(c("stat_1", "stat_2") ~ "**Treatment Received**") %>% 
  modify_footnote(all_stat_cols() ~ "Median (IQR) or Frequency (%)") %>% 
  modify_caption("**Table 1. Patient Characteristics**") %>% 
  bold_labels()
Table 1. Patient Characteristics
Variable N Overall, N = 2001 Treatment Received
Drug A, N = 981 Drug B, N = 1021
Age 189 47 (38, 57) 46 (37, 59) 48 (39, 56)
    Unknown
11 7 4
Marker Level (ng/mL) 190 0.64 (0.22, 1.39) 0.84 (0.24, 1.57) 0.52 (0.19, 1.20)
    Unknown
10 6 4
T Stage 200


    T1
53 (27%) 28 (29%) 25 (25%)
    T2
54 (27%) 25 (26%) 29 (28%)
    T3
43 (22%) 22 (22%) 21 (21%)
    T4
50 (25%) 23 (23%) 27 (26%)
Grade 200


    I
68 (34%) 35 (36%) 33 (32%)
    II
68 (34%) 32 (33%) 36 (35%)
    III
64 (32%) 31 (32%) 33 (32%)
Tumor Response 193 61 (32%) 28 (29%) 33 (34%)
    Unknown
7 3 4
Patient Died 200 112 (56%) 52 (53%) 60 (59%)
Months to Death/Censor 200 22.4 (16.0, 24.0) 23.5 (17.4, 24.0) 21.2 (14.6, 24.0)
1 Median (IQR) or Frequency (%)

Using themes e.g. to format for journals (“lancet”)

Code
theme_gtsummary_journal(journal = "lancet")
theme_gtsummary_compact()

 trial  %>% 
  tbl_summary(by = trt) %>% bold_labels()
Characteristic Drug A, N = 981 Drug B, N = 1021
Age 46 (37 – 59) 48 (39 – 56)
    Unknown 7 4
Marker Level (ng/mL) 0·84 (0·24 – 1·57) 0·52 (0·19 – 1·20)
    Unknown 6 4
T Stage

    T1 28 (29%) 25 (25%)
    T2 25 (26%) 29 (28%)
    T3 22 (22%) 21 (21%)
    T4 23 (23%) 27 (26%)
Grade

    I 35 (36%) 33 (32%)
    II 32 (33%) 36 (35%)
    III 31 (32%) 33 (32%)
Tumor Response 28 (29%) 33 (34%)
    Unknown 3 4
Patient Died 52 (53%) 60 (59%)
Months to Death/Censor 23·5 (17·4 – 24·0) 21·2 (14·6 – 24·0)
1 Median (IQR); n (%)
Code
 reset_gtsummary_theme()

Tables for models results

Code
 trial  %>% 
   glm(death~trt + age + grade, family=binomial, data=.) %>% 
  tbl_regression(exponentiate = T) %>% add_global_p()
Characteristic OR1 95% CI1 p-value
Chemotherapy Treatment

0.3
    Drug A
    Drug B 1.34 0.74, 2.40
Age 1.01 0.99, 1.03 0.4
Grade

0.056
    I
    II 1.11 0.55, 2.24
    III 2.26 1.10, 4.72
1 OR = Odds Ratio, CI = Confidence Interval

Tables for models results with univariate and adjusted results

Code
 UnivORtable <- trial %>% 
   select(
     death, trt, age, grade) %>% 
  tbl_uvregression(
    method = glm,
    y = death,
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) %>% 
 add_global_p()

 AdjORtable <- trial  %>% 
   glm(death~trt +  grade, family=binomial, data=.) %>% 
  tbl_regression(exponentiate = T)  %>% 
 add_global_p()

  tbl_merge(
    list(UnivORtable, AdjORtable),
    tab_spanner = c("**Univariable**", "**Multivariable**")
  )
Characteristic Univariable Multivariable
N OR1 95% CI1 p-value OR1 95% CI1 p-value
Chemotherapy Treatment 200

0.4

0.4
    Drug A


    Drug B
1.26 0.72, 2.22
1.26 0.71, 2.22
Age 189 1.01 0.99, 1.03 0.3


Grade 200

0.077

0.079
    I


    II
1.19 0.61, 2.35
1.18 0.60, 2.33
    III
2.17 1.08, 4.45
2.16 1.07, 4.43
1 OR = Odds Ratio, CI = Confidence Interval

The adjusted odd ratio for age is NA (95% CI NA, NA; p=0.4)