
Last chance! 50% off unlimited learning
Sale ends in
This function transforms one or more categorical variables into new categories based on specified mapping. For unmatched cases not specified in the mapping, a default value can be assigned. Missing values are retained.
recode_cat_scale(
data,
...,
assign = NULL,
other = NA,
overwrite = FALSE,
name = NULL
)
A tdcmm model or a tibble.
A tibble or a tdcmm model.
Variables to recode.
A named vector where names are the old values and values are the new values to be assigned.
The value for unmatched cases. By default, it is NA
. This
parameter is used to assign a value to cases that do not match any of the keys
in the assign
vector.
Logical. If TRUE
, it overwrites the original variable(s).
You cannot specify both 'name' and 'overwrite' parameters simultaneously.
The name of the new variable(s). If not specified, this is the same
name as the provided variable(s) but suffixed with _rec
.
Other scaling:
categorize_scale()
,
center_scale()
,
dummify_scale()
,
minmax_scale()
,
reverse_scale()
,
setna_scale()
,
z_scale()
WoJ %>%
recode_cat_scale(country,
assign = c("Germany" = 1, "Switzerland" = 2), overwrite = TRUE)
WoJ %>%
recode_cat_scale(country,
assign = c("Germany" = "german", "Switzerland" = "swiss"), other = "other",
overwrite = TRUE)
WoJ %>%
recode_cat_scale(ethics_1, ethics_2,
assign = c(`1` = 5, `2` = 4, `3` = 3, `4` = 2, `5` = 1), other = 6, overwrite = TRUE)
WoJ %>%
recode_cat_scale(ethics_1, ethics_2,
assign = c(`1` = "very low", `2` = "low", `3` = "medium", `4` = "high", `5` = "very high"),
overwrite = TRUE)
WoJ %>%
dplyr::select(temp_contract) %>% recode_cat_scale(temp_contract,
assign = c(`Permanent` = "P", `Temporary` = "T"), other = "O")
Run the code above in your browser using DataLab