Learn R Programming

DIVINE (version 0.1.1)

stats_table: Create Summary Table

Description

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
)

Value

A gtsummary table object.

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.

Examples

Run this code
# Mean ± SD summary
stats_table(
  vital_signs,
  vars = c("temperature", "saturation"),
  by = "supporto2",
  statistic_type = "mean_sd"
)

# Both mean ± SD and median [Q1; Q3]
stats_table(
 vital_signs,
 statistic_type = "both",
 include_na = FALSE
)

# Add p-value with default tests
stats_table(
 vital_signs,
 vars = c("temperature", "saturation"),
 by = "supporto2",
 pvalue = TRUE
)

# Add p-value and define method
stats_table(
 vital_signs,
 vars = c("temperature", "saturation"),
 by = "supporto2",
 pvalue = TRUE,
 test_method = list(temperature ~ "t.test")
)

Run the code above in your browser using DataLab