make_dummies: Create Dummy Variables from Categorical Columns

View source: R/make_dummies.R

make_dummiesR Documentation

Create Dummy Variables from Categorical Columns

Description

The make_dummies function takes a data frame and a vector of variable names and returns a modified data frame where the specified categorical variables have been converted into dummy variables. The original variables are removed, and the dummy variables are added to the data frame with cleaned and consistent names.

Usage

make_dummies(data, vars)

Arguments

data

A data frame containing the variables to be converted into dummy variables.

vars

A character vector specifying the names of the categorical variables to be transformed into dummy variables.

Details

This function uses model.matrix to create dummy variables for the specified categorical variables in the data. The dummy variables are created without an intercept (- 1) and are then cleaned using janitor::clean_names to ensure consistent naming. The function also uses tibble::as_tibble to return the dummy variables in tibble format. The original variables specified in vars are removed from the data frame and replaced with the newly created dummy variables.

Value

A data frame with the specified categorical variables replaced by their corresponding dummy variables.

Examples

# Example usage
data <- data.frame(
  gender = c("Male", "Female", "Female", "Male"),
  age = c(25, 30, 22, 40)
)
make_dummies(data, vars = c("gender"))


emilelatour/lamisc documentation built on March 29, 2025, 1:23 p.m.