Skip to contents

Classifies a character vector as "date", "numeric", "character", or "unknown" by applying rules in priority order.

Usage

infer_col_type(x, threshold = 0.9)

Arguments

x

Character vector to classify (as read from a CSV or FWF file).

threshold

Numeric. Minimum proportion of non-empty values that must parse as numeric for the column to be classified as "numeric". Defaults to 0.90. Configurable via type_inference_threshold in rule_overrides.

Value

A single character string: "date", "numeric", "character", or "unknown".

Details

Date formats are tried in this fixed precedence order: "%Y-%m-%d", "%d/%m/%Y", "%m/%d/%Y", "%Y%m%d", "%d-%m-%Y", and the two ISO-style date-time shapes "%Y-%m-%d %H:%M:%S" and "%Y-%m-%dT%H:%M:%S" (a timestamp column classifies as "date"; the type is a label with no time-of-day-aware stat or drift, so the time part carries no separate semantics). A column is classified as "date" only when every non-empty value both matches that format's exact character shape and parses as a valid calendar date; a single malformed date therefore flips the whole column to "numeric" or "character" (such flips between deliveries are surfaced by check CP-02c). The shape is anchored, so a value with trailing characters ("2024-01-15x") or extra digits (the 9-digit "202401159") is not treated as a date. Two caveats follow from the precedence rules: ambiguous day/month values resolve day-first ("%d/%m/%Y" is tried before "%m/%d/%Y"), and all-8-digit identifier columns whose values happen to be valid "%Y%m%d" dates classify as dates. Pin the type with an entry in the column_types config map when the heuristic gets a column wrong.

Examples

infer_col_type(c("2024-01-01", "2024-06-15"))   # "date"
#> [1] "date"
infer_col_type(c("2024-01-01 09:59:22"))         # "date" (date-time)
#> [1] "date"
infer_col_type(c("1.5", "2.0", "3.1"))          # "numeric"
#> [1] "numeric"
infer_col_type(c("high", "low", "medium"))       # "character"
#> [1] "character"
infer_col_type(c(NA, "", NA))                    # "unknown"
#> [1] "unknown"
infer_col_type(c(rep("1", 17), "a", "b", "c"), threshold = 0.80)  # "numeric"
#> [1] "numeric"