Learn R Programming

dentomedical (version 0.2.0)

sum_cor: Summarize Correlations Between a Reference Variable and Others

Description

Computes correlations between a reference variable and one or more comparison variables. For Pearson correlations, 95% confidence intervals are also calculated. Can optionally stratify by a grouping variable. Returns a formatted flextable and optionally prints a narrative summary describing weak, moderate, and strong correlations.

Usage

sum_cor(
  data,
  ref_var,
  compare_vars,
  by = NULL,
  method = "pearson",
  digits = 3,
  report = TRUE
)

Value

A flextable object showing correlations, 95% CI (Pearson only), p-values, and interpretation.

Arguments

data

A data frame containing the variables of interest.

ref_var

The reference variable (numeric) for correlation calculations.

compare_vars

A character vector of variables to correlate with the reference variable.

by

Optional grouping variable. If provided, correlations are calculated within each group.

method

Correlation method. Options: "pearson", "spearman", or "kendall" (default: "pearson").

digits

Number of decimal places to round correlation coefficients and CIs (default: 3).

report

Logical. If TRUE, prints a narrative summary of correlations (default: TRUE).

Examples

Run this code
# Example 1: Correlations across entire dataset
sum_cor(
  data = iris,
  ref_var = "Sepal.Length",
  compare_vars = c("Petal.Length", "Petal.Width", "Sepal.Width"),
  method = "pearson",
  digits = 2,
  report = TRUE
)

# Example 2: Correlations by Species
sum_cor(
  data = iris,
  ref_var = "Sepal.Length",
  by = "Species",
  compare_vars = c("Petal.Length", "Petal.Width", "Sepal.Width"),
  method = "pearson",
  digits = 2,
  report = TRUE
)

Run the code above in your browser using DataLab