ChatGPT for R programming: expose unknown labels before making a factor

When an R factor suddenly contains NA values, show ChatGPT the original character values and the explicit level list. A label that does not match the supplied levels becomes NA during factor construction. Keep the raw column and identify unmatched nonmissing labels before converting, so a spelling difference does not become indistinguishable from an originally missing response.

Level order also carries meaning. If the intended sequence is Low, Medium, High, declare that sequence rather than accepting the default sorted order.

Keep a raw value beside each category

This original fictional review-priority fixture contains three accepted labels, one abbreviated label whose mapping has not been approved, and one genuinely missing value:

raw_priority <- c("Low", "High", "Medium", "Med", NA_character_)
allowed <- c("Low", "Medium", "High")
unmatched <- !is.na(raw_priority) & !(raw_priority %in% allowed)
which(unmatched)

The expected unmatched position is 4, whose raw value is Med. Ask the data owner whether Med means Medium; the resemblance is not an approved mapping. Position 5 was already missing and should not be reported as an unknown spelling.

The R factor reference, checked October 24, 2026, describes matching source values against levels, replacing unmatched values with NA, and using the level order for an ordered factor. The next step demonstrates that behavior on a copy:

priority <- factor(raw_priority, levels = allowed, ordered = TRUE)
stopifnot(identical(levels(priority), allowed))
stopifnot(identical(which(is.na(priority)), c(4L, 5L)))
stopifnot(identical(which(is.na(raw_priority)), 5L))
table(priority, useNA = "always")

The expected counts are Low=1, Medium=1, High=1 and NA=2. Retaining raw_priority explains why the two NA entries have different origins. Converting the source in place would remove that evidence unless it had been preserved elsewhere.

Internal codes are not measured values

With the declared level order, as.numeric(priority) exposes codes 1, 3, 2, NA, NA. Those numbers identify levels; they are not a measured priority score or evidence that High is exactly three times Low. Reordering the levels changes the codes without changing the underlying category labels.

Do not repair an unexpected numeric column by guessing that the factor codes are original measurements. For modeling, preserve the intended categorical specification and review the actual model's treatment of ordered factors before interpreting coefficients. This small encoding exercise does not establish a statistical model or a valid numerical scale.

Ask for a conversion with an exception report

Review this R categorical conversion before changing the source.
R version and existing code: [PASTE]
Raw character vector with stable row IDs: [PASTE]
Allowed categories in intended order: [ENTER]
Whether the categories are ordered, and why: [ENTER]
Approved spelling mappings: [PASTE OR NONE]
Policy for genuine missing values and unknown labels: [ENTER]

Find unmatched nonmissing labels before factor construction. Retain
the original values and IDs, propose a separate factor column, and
report newly introduced NA values separately from original missing
values. Apply only approved mappings. Show levels, category counts
including NA, and assertions for the supplied cases. Explain any use
of integer codes; do not treat them as measurements or invent a model.
Do not claim R or ChatGPT executed the code without actual results.

The approved dictionary is the essential input: asking only to “fix the NA values” invites a guess about what the source meant.

Run the code in your R environment and compare the unmatched rows, factor levels and missing-value counts. This article gives authored expected R results, not a captured R session. Once the data owner approves any correction, retain that mapping with the raw categories and rerun the checks. For broader file transformations, the data-cleaning guide keeps the same source-preservation rule while handling other field types.

Keep reading