Learn R Programming

dentomedical (version 0.2.0)

sum_crosstab: Crosstabulation with Counts and Percentages

Description

Creates a cross-tabulation (contingency table) between two categorical variables and returns a publication-ready table as a flextable. The table displays cell counts with optional percentages (cell-wise, row-wise, or column-wise), and includes row totals (right) and column totals (bottom).

Usage

sum_crosstab(
  data,
  row_var,
  col_var,
  percent = c("none", "cell", "row", "col"),
  digits = 2
)

Value

A flextable object containing the formatted cross-tabulation with counts, optional percentages, and marginal totals.

Arguments

data

A data frame containing the variables to be cross-tabulated.

row_var

A character string specifying the row variable (categorical).

col_var

A character string specifying the column variable (categorical).

percent

Type of percentage to display alongside counts. One of "none" (counts only), "cell" (percentage of total), "row" (row percentage), or "col" (column percentage).

digits

Integer specifying the number of decimal places for percentages.

Examples

Run this code
data(CO2)

sum_crosstab(
  data = CO2,
  row_var = "Treatment",
  col_var = "Type",
  percent = "cell",
  digits = 2
)

Run the code above in your browser using DataLab