encode_ordinals {eHDPrep} | R Documentation |
Encode ordinal variables
Description
Converts character or factor variables in the input data frame to ordered factors embedding numeric relationship between values while preserving value labels.
Usage
encode_ordinals(data, ord_levels, ..., strict_levels = TRUE)
Arguments
data |
A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). |
ord_levels |
character vector containing values in desired order (lowest to highest). |
... |
< |
strict_levels |
logical constant. If |
Value
dataframe with specified variables encoded as ordered factors.
Examples
data(example_data)
require(dplyr)
require(magrittr)
encode_ordinals(example_data, ord_levels = c("N0","N1","N2"), n_stage)
# Note: "unequivocal" is present in t_stage but not in `ord_levels`.
# with `strict_levels` TRUE, t_stage is unmodified and a warning message is given:
encode_ordinals(example_data,
ord_levels = c("T1","T2","T3a", "T3b", "T4"), strict_levels = TRUE, t_stage) %>%
select(t_stage)
# with `strict_levels` FALSE, it is replaced with NA:
encode_ordinals(example_data,
ord_levels = c("T1","T2","T3a", "T3b", "T4"), strict_levels = FALSE, t_stage) %>%
select(t_stage)