1  Your first table: the basics of tbl_summary()

1.1 Looking at the data

1.2 Table 1 in one line

With just this,

  • continuous variables (age, marker, etc.) → median (Q1, Q3)
  • categorical variables (stage, grade, etc.) → n (%)
  • 0/1 variables (response, death) → the number and proportion of 1s on one row
  • missing values → an Unknown row

are detected and summarized automatically. The footnote below the table states the summary statistics used.

1.3 Choosing variables: include

In include, besides listing column names directly, you can use selection helpers such as starts_with("t") and where(is.numeric).

1.4 Changing labels: label

In gtsummary, per-variable settings are written as variable ~ value (a formula). Several can be specified together with list(). You will see this notation again and again.

1.5 Changing statistics: statistic

To show continuous variables as mean (SD):

  • all_continuous(): all continuous variables
  • all_categorical(): all categorical variables (including 0/1 variables)
  • Write statistic names inside { }; any other characters are displayed as is
Available statistics (continuous) Meaning
{mean}, {sd} Mean, standard deviation
{median}, {p25}, {p75} Median, 25th and 75th percentiles
{min}, {max} Minimum, maximum
{N_nonmiss}, {N_miss} Number non-missing, number missing
Available statistics (categorical) Meaning
{n}, {N}, {p} Count, denominator, percentage (%)
TipDifferent statistics for different variables

You can also specify individual variable names, as in statistic = list(age ~ "{mean} ({sd})", marker ~ "{median} ({p25}, {p75})"). This lets you, for example, use mean (SD) for roughly normal variables and median (IQR) for skewed ones.

1.6 Showing on multiple rows: continuous2

To display several statistics for one variable on separate rows, set its type to continuous2.

1.7 Number of decimal places: digits

1.8 Specifying variable types: type

gtsummary detects types automatically from the kinds of values, but when it doesn’t do what you want, fix it with type.

Type Display
"continuous" Summary statistics on one row
"continuous2" Summary statistics on several rows
"categorical" n (%) for each level
"dichotomous" Only one level (by default 1, TRUE, or “Yes”) on one row

1.9 Displaying missing values: missing

Warning

With missing = "no", the missing row disappears, but the denominator of the percentages is the number excluding missing values. Papers are often required to report the number missing, so if you hide it, supplement it in a footnote or similar.

1.10 Direction of percentages and sorting

ImportantChapter summary
  • tbl_summary() alone gives you the groundwork for Table 1
  • include (variable selection), label (labels), statistic (statistics), digits (decimal places), type (type), missing (missing values)
  • Settings are written by combining variable ~ value with all_continuous() / all_categorical()