Skip to contents

Creates comprehensive tables for categorical or continuous variables with formatting, statistical tests, prevalence ratios (PR), odds ratios (OR), and column stratification. Supports both univariate and bivariate analysis with flexible output formatting.

Usage

tb(
  data,
  ...,
  m = FALSE,
  d = 1,
  format = TRUE,
  style = "n_pct",
  style.rp = "{rp} ({lower} - {upper})",
  style.or = "{or} ({lower} - {upper})",
  test = FALSE,
  subset = NULL,
  strat = NULL,
  rp = FALSE,
  or = FALSE,
  ref = NULL,
  conf.level = 0.95,
  var.type = NULL,
  stat.cont = "median"
)

Arguments

data

A data.frame or atomic vector.

...

Variables to be tabulated. Can include variable names and/or flags (m, p, row, col, rp, or) for controlling output format.

m

Logical. If TRUE, includes missing values (NA) in the table. Default: FALSE.

d

Integer. Number of decimal places for percentages and statistics. Default: 1.

format

Logical. If TRUE, renders a formatted grid output (Stata-style). If FALSE, prints as simple matrix. Default: TRUE.

style

Character. Format for displaying counts and percentages. Options:

  • "n_pct": "100 (25.0\

  • "pct_n": "25.0\

  • Custom template using {n} and {p} placeholders, e.g., "{n} [{p}%]".

style.rp

Character. Format string for Prevalence Ratio. Default: "{rp} ({lower} - {upper})".

style.or

Character. Format string for Odds Ratio. Default: "{or} ({lower} - {upper})".

test

Logical or Character. Performs statistical test on 2x2+ tables.

  • TRUE: Automatic selection (chi-squared for categorical)

  • "chisq", "fisher", "mcnemar"

subset

Logical expression for row filtering.

strat

Variable for column stratification. Disables PR/OR calculations.

rp

Logical. If TRUE, calculates Prevalence Ratios (PR).

or

Logical. If TRUE, calculates Odds Ratios (OR).

ref

Character or numeric. Specifies the reference level for PR/OR calculations.

conf.level

Numeric. Confidence level for intervals (0-1). Default: 0.95.

var.type

Named character vector specifying variable types.

  • select the line variable and choose between "continuous" and "cont"

  • ex: tb(epitabl, age, sex, var.type = c(age = "continuous"))

stat.cont

Character. "mean" (Mean/SD) or "median" (Median/IQR).

Value

An object of class tb.