Learn R Programming

dentomedical (version 0.2.0)

sum_stat_p: Summarize Continuous and Categorical Variables with Grouping and P-Values

Description

sum_stat_p generates a descriptive summary table for both continuous and categorical variables, stratified by a grouping variable. It automatically computes appropriate statistical tests (Chi-square, Fisher's exact, t-test, Wilcoxon, ANOVA, or Kruskal–Wallis) based on variable type, number of groups, and data distribution. Continuous variables can be summarized as mean (SD) or median (IQR), and categorical variables as counts and percentages.

Usage

sum_stat_p(data, by, statistic = "mean_sd", test_type = "auto", paired = FALSE)

Value

A flextable object displaying a publication-ready summary table including:

  • Counts and percentages for categorical variables

  • Mean (SD) or median (IQR) for continuous variables

  • P-values for group comparisons

  • Footnotes describing which summary statistics and tests were used

Arguments

data

A data.frame or tibble containing the variables to summarize.

by

A string specifying the grouping variable for stratified summaries.

statistic

Character string indicating how to summarize continuous variables: "mean_sd" (default) or "med_iqr".

test_type

Character string specifying the statistical test for group comparisons: "auto" (default, chooses test based on data), "t.test", "wilcox", "anova", "kruskal", "chisq", or "fisher".

paired

Logical. If TRUE and only two groups exist, performs a paired t-test for continuous variables. Default is FALSE.

Details

The output is formatted as a flextable with footnotes indicating which summary statistics were used and which statistical tests were applied.

Examples

Run this code
# Summary of iris dataset by species
sum_stat_p(iris, by = "Species", statistic = "mean_sd", test_type = "auto")

# Summary of CO2 dataset by Type with paired t-test
sum_stat_p(CO2, by = "Type", statistic = "mean_sd", test_type = "t.test", paired = TRUE)

# Summary using median and IQR
sum_stat_p(iris, by = "Species", statistic = "med_iqr", test_type = "kruskal")

Run the code above in your browser using DataLab