recode_cat_scale: Recode one or more categorical variables into new categories

recode_cat_scaleR 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 NA. This parameter is used to assign a value to cases that do not match any of the keys in the assign vector.

overwrite

Logical. If TRUE, it overwrites the original variable(s). You cannot specify both 'name' and 'overwrite' parameters simultaneously.

name

The name of the new variable(s). If not specified, this is the same name as the provided variable(s) but suffixed with ⁠_rec⁠.

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")

joon-e/tidycomm documentation built on Feb. 24, 2024, 8:58 a.m.