recode_cat_scale {tidycomm} | R Documentation |
Recode one or more categorical variables into new categories
Description
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.
Usage
recode_cat_scale(
data,
...,
assign = NULL,
other = NA,
overwrite = FALSE,
name = NULL
)
Arguments
data |
A tibble or a tdcmm model. |
... |
Variables to recode. |
assign |
A named vector where names are the old values and values are the new values to be assigned. |
other |
The value for unmatched cases. By default, it is |
overwrite |
Logical. If |
name |
The name of the new variable(s). If not specified, this is the same
name as the provided variable(s) but suffixed with |
Value
A tdcmm model or a tibble.
See Also
Other scaling:
categorize_scale()
,
center_scale()
,
dummify_scale()
,
minmax_scale()
,
reverse_scale()
,
setna_scale()
,
z_scale()
Examples
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")