Skip to contents

This function creates a summary comparison table from a given data frame. It summarizes continuous variables with their mean (and standard deviation) and categorical variables with counts and percentages. Comparisons between groups are performed based on a specified grouping (comparison) variable.

Usage

MakeComparisonTable(
  DataFrame,
  Variables = NULL,
  CompVariable,
  ValueDigits = 2,
  pDigits = 3
)

Arguments

DataFrame

A data frame containing the data for generating the comparison table.

Variables

An optional character vector of variable names (besides the grouping variable) to include in the comparison table. If NULL, all variables in DataFrame are used.

CompVariable

A character string specifying the name of the variable used to divide the data into comparison groups.

ValueDigits

An integer specifying the number of digits to display for continuous variable statistics (mean and standard deviation). Default is 2.

pDigits

An integer specifying the number of digits to display for p-values. Default is 3.

Value

A tbl_summary object containing the comparison table.

Details

The function first determines the set of variables to analyze. If Variables is NULL, all variables from DataFrame are used; otherwise, it ensures that CompVariable is included once in the analysis. Next, it filters the data frame to include only the selected variables and excludes any factor variables with more than 15 unique levels. The summary is then created using tbl_summary with continuous variables summarized as "mean (sd)" and categorical variables summarized as "n (p)". Additional information, such as sample sizes and p-values, is appended to the table.

Note

This wrapper function is adapted from code written by Aparna Bhattacharyya.