make_dummies | R Documentation |
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.
make_dummies(data, vars)
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. |
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.
A data frame with the specified categorical variables replaced by their corresponding dummy variables.
# Example usage
data <- data.frame(
gender = c("Male", "Female", "Female", "Male"),
age = c(25, 30, 22, 40)
)
make_dummies(data, vars = c("gender"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.