Skip to contents

Replaces various missing value representations with a consistent symbol (default "---") so it is easier to tell actual data from missing or undefined values.

Works with all gtsummary table types, including stacked tables (tbl_strata) and survey-weighted summaries (tbl_svysummary). Handles tables with or without the standard var_type column.

Usage

clean_table(tbl, symbol = "---")

Arguments

tbl

A gtsummary table object (e.g., from tbl_summary(), tbl_svysummary(), tbl_regression(), or tbl_strata())

symbol

Character string to replace missing values with. Default is "---" (em-dash style). Common alternatives: "\u2014" (em-dash), "\u2013" (en-dash), "--", or "N/A".

Value

A gtsummary table object with standardized missing value display

Details

The function uses gtsummary::modify_table_body() to transform character columns and replace missing, undefined, and zero-valued patterns with a consistent symbol. Matched patterns include:

  • Literal NA and Inf / -Inf values

  • Count/percent pairs: "0 (0%)", "0 (NA%)", "0 (NA)", "NA (0)", "NA (NA)"

  • Decimal variants: "0.00 (0.00)", "0.00% (0.00)", "0% (0.000)"

  • Paired values: "NA, NA"

  • Confidence intervals: "NA (NA, NA)", "0% (0.000) (0%, 0%)", "0.00 (0.00) (0.00, 0.00)", and similar zero-CI patterns

Replacing these patterns with a single symbol keeps the table easier to read.

Note: The function checks for the presence of var_type column before applying modify_missing_symbol(). This allows it to work with tbl_strata objects which use var_type_1, var_type_2, etc. instead of var_type.

See also

Examples

# \donttest{
# Basic usage - clean missing values in summary table
demo_trial <- gtsummary::trial |>
  dplyr::mutate(
    age = dplyr::if_else(trt == "Drug B", 0, age),
    marker = dplyr::if_else(trt == "Drug A", NA, marker)
  ) |>
  dplyr::select(trt, age, marker)

demo_trial |>
  gtsummary::tbl_summary(by = trt) |>
  clean_table()
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
age 46 (37, 60) —
    Unknown 7 0
marker — 0.52 (0.18, 1.21)
    Unknown 98 4
1 Median (Q1, Q3)
# Used inside extras() automatically demo_trial |> gtsummary::tbl_summary(by = trt) |> extras()
Overall
N = 200
1
Drug A
N = 98
1
Drug B
N = 102
1
p-value2
age 0 (0, 45) 46 (37, 60) — <0.001
    Unknown 7 7 0
marker 0.52 (0.18, 1.21) — 0.52 (0.18, 1.21)
    Unknown 102 98 4
1 Median (Q1, Q3)
2 Wilcoxon rank sum test; NA
# Custom missing symbol demo_trial |> gtsummary::tbl_summary(by = trt) |> clean_table(symbol = "???")
Characteristic Drug A
N = 98
1
Drug B
N = 102
1
age 46 (37, 60) ???
    Unknown 7 0
marker ??? 0.52 (0.18, 1.21)
    Unknown 98 4
1 Median (Q1, Q3)
# }