Converts character columns to numeric and numeric columns to character
where the var_spec data_type requires it. Factor columns are
always coerced to the appropriate base type. Preserves all non-class
attributes (labels, formats) across coercion. Warns when coercion
introduces NA values.
See also
Other specification:
apply_spec(),
decode_var(),
detect_adam_class(),
detect_adam_classes(),
drop_unspec_vars(),
herald_spec(),
order_cols(),
read_spec(),
read_spec_define(),
scaffold_vars(),
sort_keys(),
spec_codelist(),
spec_datasets(),
spec_study(),
spec_vars(),
write_define_html(),
write_define_xml(),
write_spec()
Examples
spec <- herald_spec(
ds_spec = data.frame(dataset = "DM", label = "Demographics",
stringsAsFactors = FALSE),
var_spec = data.frame(
dataset = "DM",
variable = c("STUDYID", "AGE"),
data_type = c("text", "integer"),
stringsAsFactors = FALSE
)
)
dm <- data.frame(STUDYID = "S1", AGE = "65", stringsAsFactors = FALSE)
result <- coerce_types(dm, spec, "DM")
#> Coerced 1 variable: AGE (char→num)
is.numeric(result$AGE) # TRUE
#> [1] TRUE
is.character(result$STUDYID) # TRUE
#> [1] TRUE
