Summary Tables
# Create a basic summary table
trial %>%
select(trt, age, grade, response) %>%
tbl_summary()
| Characteristic |
N = 200 |
| Chemotherapy Treatment |
|
| Drug A |
98 (49%) |
| Drug B |
102 (51%) |
| Age |
47 (38, 57) |
| Unknown |
11 |
| Grade |
|
| I |
68 (34%) |
| II |
68 (34%) |
| III |
64 (32%) |
| Tumor Response |
61 (32%) |
| Unknown |
7 |
# Split table by group
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt)
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
46 (37, 59) |
48 (39, 56) |
| Unknown |
7 |
4 |
| 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 |
# Skip missing data separately
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no")
| Characteristic |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
46 (37, 59) |
48 (39, 56) |
| Grade |
|
|
| I |
35 (36%) |
33 (32%) |
| II |
32 (33%) |
36 (35%) |
| III |
31 (32%) |
33 (32%) |
| Tumor Response |
28 (29%) |
33 (34%) |
# Add column with total number of non-missing observations
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n()
| Characteristic |
N |
Drug A, N = 98 |
Drug B, N = 102 |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
| Grade |
200 |
|
|
| I |
|
35 (36%) |
33 (32%) |
| II |
|
32 (33%) |
36 (35%) |
| III |
|
31 (32%) |
33 (32%) |
| Tumor Response |
193 |
28 (29%) |
33 (34%) |
# Test for a difference between groups
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p()
| Characteristic |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Update the column header
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**")
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Text formatting: Bold labels
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels()
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Text formatting: Italicize Levels
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels() %>%
italicize_levels()
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Table formatting: Pretty print with gt() function
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels() %>%
italicize_levels() %>%
as_gt()
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Table formatting: Add title
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels() %>%
italicize_levels() %>%
as_gt() %>%
gt::tab_header(title = "Table 1. Summary table of 200 patients who received Drug A or Drug B")
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Table formatting: Add Markdown & Bold Title
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels() %>%
italicize_levels() %>%
as_gt() %>%
gt::tab_header(title = gt::md("**Table 1. Summary table of 200 patients who received Drug A or Drug B**"))
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |
# Table formatting: Add Markdown & Italic Title
trial %>%
select(trt, age, grade, response) %>%
tbl_summary(by = trt, missing = "no") %>%
add_n() %>%
add_p() %>%
modify_header(label = "**Variables**") %>%
bold_labels() %>%
italicize_levels() %>%
as_gt() %>%
gt::tab_header(title = gt::md("*Table 1. Summary table of 200 patients who received Drug A or Drug B*"))
| Variables |
N |
Drug A, N = 98 |
Drug B, N = 102 |
p-value |
| Age |
189 |
46 (37, 59) |
48 (39, 56) |
0.7 |
| 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 |