Library

library(gtsummary)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Data

#View(trial)
head(trial)
str(trial)
## tibble [200 × 8] (S3: tbl_df/tbl/data.frame)
##  $ trt     : chr [1:200] "Drug A" "Drug B" "Drug A" "Drug A" ...
##   ..- attr(*, "label")= chr "Chemotherapy Treatment"
##  $ age     : num [1:200] 23 9 31 NA 51 39 37 32 31 34 ...
##   ..- attr(*, "label")= chr "Age"
##  $ marker  : num [1:200] 0.16 1.107 0.277 2.067 2.767 ...
##   ..- attr(*, "label")= chr "Marker Level (ng/mL)"
##  $ stage   : Factor w/ 4 levels "T1","T2","T3",..: 1 2 1 3 4 4 1 1 1 3 ...
##   ..- attr(*, "label")= chr "T Stage"
##  $ grade   : Factor w/ 3 levels "I","II","III": 2 1 2 3 3 1 2 1 2 1 ...
##   ..- attr(*, "label")= chr "Grade"
##  $ response: int [1:200] 0 1 0 1 1 0 0 0 0 0 ...
##   ..- attr(*, "label")= chr "Tumor Response"
##  $ death   : int [1:200] 0 0 0 1 1 1 0 1 0 1 ...
##   ..- attr(*, "label")= chr "Patient Died"
##  $ ttdeath : num [1:200] 24 24 24 17.6 16.4 ...
##   ..- attr(*, "label")= chr "Months to Death/Censor"

Cross Table

trial %>% 
  tbl_summary(
    by=trt
  )
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
Age 46 (37, 60) 48 (39, 56)
    Unknown 7 4
Marker Level (ng/mL) 0.84 (0.23, 1.60) 0.52 (0.18, 1.21)
    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.5, 24.0)
1 Median (Q1, Q3); n (%)

Add Statistic

trial %>% 
  tbl_summary(
    by=trt,
    statistic = list(
      all_categorical()~"{n}/{N} ({p}%)",
      all_continuous()~"{mean} ({sd})"
  )) %>% 
  add_p() %>% 
  add_overall() %>% 
  add_n()
Characteristic N Overall
N = 200
1
Drug A
N = 98
1
Drug B
N = 102
1
p-value2
Age 189 47 (14) 47 (15) 47 (14) 0.7
    Unknown
11 7 4
Marker Level (ng/mL) 190 0.92 (0.86) 1.02 (0.89) 0.82 (0.83) 0.085
    Unknown
10 6 4
T Stage 200


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


0.9
    I
68/200 (34%) 35/98 (36%) 33/102 (32%)
    II
68/200 (34%) 32/98 (33%) 36/102 (35%)
    III
64/200 (32%) 31/98 (32%) 33/102 (32%)
Tumor Response 193 61/193 (32%) 28/95 (29%) 33/98 (34%) 0.5
    Unknown
7 3 4
Patient Died 200 112/200 (56%) 52/98 (53%) 60/102 (59%) 0.4
Months to Death/Censor 200 19.6 (5.3) 20.2 (5.0) 19.0 (5.5) 0.14
1 Mean (SD); n/N (%)
2 Wilcoxon rank sum test; Pearson’s Chi-squared test