infer_spec: Infer a column specification for a source table

View source: R/spec.r

infer_specR Documentation

Infer a column specification for a source table

Description

Heuristics, in order: all-missing columns are empty; trial and EDC key names (USUBJID, SUBJID, SITEID, STUDYID, RANDNO, ...) are id; identifier-like names with near-unique values (or code-like values such as AB123) are id; a single value with no missing cells is constant; mostly-date columns are date; mostly-numeric columns are numeric, or categorical if they take few distinct values (coded 0/1/2 fields); remaining character columns are categorical when the label set is small and text otherwise.

Usage

infer_spec(
  src,
  id = NULL,
  text = NULL,
  roles = NULL,
  id_pattern = NULL,
  text_pattern = NULL,
  max_levels = 10,
  max_cat_levels = 60,
  warn_unknown = TRUE
)

Arguments

src

A synth_source from read_source().

id, text

Column names (internal or on-disk, case-insensitive) to force to id or text, whatever the heuristics say.

roles

Named character vector of further role overrides.

id_pattern, text_pattern

Regular expressions on column names, e.g. id_pattern = "^(pat|subj).*(id|no)$". See apply_role_overrides(), also for session-wide defaults via options().

max_levels

Numeric columns with at most this many distinct values are treated as categorical.

max_cat_levels

Character columns with at most this many labels (and at most 10% of the observed rows) are categorical, otherwise text.

warn_unknown

Warn about override names that match no column.

Value

A data.frame, one row per column: column, orig_name, role, n_obs, n_unique, miss_frac, num_frac, date_frac, reason.

Examples

df <- data.frame(id = sprintf("AB%03d", 1:60), value = 1:60,
                 code = rep(c(0, 1, NA), 20), group = rep(c("A", "B"), 30))
path <- file.path(tempdir(), "infer_spec.csv")
utils::write.csv(df, path, row.names = FALSE, na = "")
src <- read_source(path)
spec <- infer_spec(src, id = "id", roles = c(code = "categorical"))
spec[, c("column", "role", "reason")]

cellsynth documentation built on Oct. 31, 2026, 1:07 a.m.