Skip to contents

This function generates a summary table using the gtsummary package. It allows customization of the reported statistics for continuous variables and categorical variables. Users can optionally include p-values for group comparisons and manage the reporting of missing values.

Usage

stats_table(
  data,
  vars = NULL,
  var_labels = NULL,
  by = NULL,
  statistic_type = "mean_sd",
  pvalue = FALSE,
  test_method = NULL,
  include_na = TRUE
)

Arguments

data

A data frame containing the dataset.

vars

A character vector of variable names to include in the summary. If NULL (default), all variables are included.

var_labels

A list of labels to replace variable names in the table.

by

A character string specifying a grouping variable. If NULL (default), no grouping is applied.

statistic_type

A character string specifying the type of statistic to report for continuous variables. Options are:

  • "mean_sd": Mean (SD) for continuous variables.

  • "median_iqr": Median (Q1; Q3) for continuous variables.

  • "both": Both Mean (SD) and Median (Q1; Q3).

pvalue

A logical value indicating whether to include p-values in the summary. Defaults to FALSE.

test_method

Optional. Only used if pvalue = TRUE. A list specifying custom statistical tests for each variable. If NULL, gtsummary will choose default tests based on variable type.

include_na

A logical value indicating whether to include rows with missing values in the output. Defaults to TRUE.

Value

A gtsummary table object.

Examples

# Mean ± SD summary
stats_table(
  vital_signs,
  vars = c("temperature", "saturation"),
  by = "supporto2",
  statistic_type = "mean_sd"
)
#> 5 missing rows in the "supporto2" column have been removed.
Variables No
N = 3,971
1
Yes
N = 1,837
1
Temperature 36.99 (0.96) 36.89 (0.96)
    Unknown 77 23
Oxygen saturation 93.94 (5.70) 94.45 (5.31)
    Unknown 55 5
1 Mean (SD)
# Both mean ± SD and median [Q1; Q3] stats_table( vital_signs, statistic_type = "both", include_na = FALSE )
Variables N = 5,813
Record ID
    Mean (SD) 2,907.00 (1,678.21)
    Median [Q1;Q3] 2,907.00 [1,454.00;4,360.00]
COVID wave, n (%)
    Wave 1 3,406 (58.59%)
    Wave 2 786 (13.52%)
    Wave 3 768 (13.21%)
    Wave 4 853 (14.67%)
Medical Center, n (%)
    Hospital A 1,275 (21.93%)
    Hospital B 2,434 (41.87%)
    Hospital C 1,103 (18.97%)
    Hospital D 463 (7.96%)
    Hospital E 538 (9.26%)
Temperature
    Mean (SD) 36.96 (0.96)
    Median [Q1;Q3] 36.80 [36.20;37.60]
FiO2
    Mean (SD) 29.11 (19.08)
    Median [Q1;Q3] 21.00 [21.00;26.00]
Systolic blood pressure
    Mean (SD) 130.40 (21.02)
    Median [Q1;Q3] 129.00 [117.00;144.00]
Diastolic blood pressure
    Mean (SD) 74.15 (13.23)
    Median [Q1;Q3] 74.00 [65.00;82.00]
Oxygen saturation
    Mean (SD) 94.10 (5.58)
    Median [Q1;Q3] 95.00 [93.00;97.00]
Heart rate
    Mean (SD) 88.72 (17.75)
    Median [Q1;Q3] 87.00 [77.00;100.00]
Oxygen Support, n (%) 1,837 (31.63%)
Normal X-ray, n (%) 463 (8.05%)
Pleural effusion, n (%) 121 (2.80%)
Oxygen Saturation to FiO2 Ratio
    Mean (SD) 388.48 (109.28)
    Median [Q1;Q3] 442.86 [357.14;457.14]
# Add p-value with default tests stats_table( vital_signs, vars = c("temperature", "saturation"), by = "supporto2", pvalue = TRUE ) #> 5 missing rows in the "supporto2" column have been removed. #> The following errors were returned during `add_p()`: #> For variable `saturation` (`supporto2`) and "estimate", "std.error", #> "parameter", "statistic", "conf.low", "conf.high", and "p.value" statistics: #> The package "broom" (>= 1.0.8) is required. #> For variable `temperature` (`supporto2`) and "estimate", "std.error", #> "parameter", "statistic", "conf.low", "conf.high", and "p.value" statistics: #> The package "broom" (>= 1.0.8) is required.
Variables No
N = 3,971
1
Yes
N = 1,837
1
p-value
Temperature 36.99 (0.96) 36.89 (0.96)
    Unknown 77 23
Oxygen saturation 93.94 (5.70) 94.45 (5.31)
    Unknown 55 5
1 Mean (SD)
# Add p-value and define method stats_table( vital_signs, vars = c("temperature", "saturation"), by = "supporto2", pvalue = TRUE, test_method = list(temperature ~ "t.test") ) #> 5 missing rows in the "supporto2" column have been removed. #> The following errors were returned during `add_p()`: #> For variable `saturation` (`supporto2`) and "estimate", "std.error", #> "parameter", "statistic", "conf.low", "conf.high", and "p.value" statistics: #> The package "broom" (>= 1.0.8) is required. #> For variable `temperature` (`supporto2`) and "estimate", "std.error", #> "parameter", "statistic", "conf.low", "conf.high", and "p.value" statistics: #> The package "broom" (>= 1.0.8) is required.
Variables No
N = 3,971
1
Yes
N = 1,837
1
p-value
Temperature 36.99 (0.96) 36.89 (0.96)
    Unknown 77 23
Oxygen saturation 93.94 (5.70) 94.45 (5.31)
    Unknown 55 5
1 Mean (SD)