Skip to contents

This function creates new variables representing effect codes for selected factors. It can also be used to generate dummy codes instead.

Usage

make_factors_into_effect_codes(
  df,
  factors,
  remove_selected_columns = TRUE,
  ignore_na = TRUE,
  change_which_dummy_is_removed = FALSE,
  make_into_dummy_instead = c()
)

Arguments

df

A data.frame

factors

Vector of factor names found in df

remove_selected_columns

(logical) Whether to remove original factors in factors (default TRUE)

ignore_na

(logical) Whether to ignore NA. If FALSE, NA will be treated as a factor level (default TRUE)

change_which_dummy_is_removed

(logical) Randomize factor levels prior to creating codes (default FALSE)

make_into_dummy_instead

Vector of variables in factors for which to obtain dummy codes instead.

Value

df with effect codes added (and original factors possibly removed)

Details

This function uses the library fastDummies to generate dummy codes, then recodes the dummy codes into effect codes (except for variables whose name is also specified in make_into_dummy_instead).

Examples

if (FALSE) {
df |> make_factors_into_effect_codes(
  factors=c("Gender", "Nationality"),
  make_into_dummy_instead = "Gender"
)
}