dummy_recodes | R Documentation |
Create dummy variables for the factor variables in the dataset. The function is based on the functions in R 'dummies' package.
dummy_recodes(data = data, sep = "_", drop = FALSE, all = FALSE)
data |
An object such as a data.frame or matrix that has colnames. |
sep |
For the names of the created dummy variables, sep is the character used between the variable name and the value. Default: "_". |
drop |
Logical. Whether to drop one level with smallest mean value. This is very useful to create dummy variables for regression since we can at most keep n-1 levels of a variable in the model. Default: False. |
all |
Logical. Whether to return columns that are not dummy classes. The default is FALSE and not returns all classes. Non dummy classes are dropped |
A synthetic data frame with the dummy variables. If all is TRUE, columns that are not dummy classes will be returned as well. If drop is TRUE, only (n-1) dummy variables will be created for one factor variable.
mag <- data(mag) var.factor <- c("NFAC1_2", "NFAC2_2", "NFAC3_2", "NFAC4_2", "NFAC5_2", "NFAC6_2", "NFAC7_2", "childhh", "ethnic", "maritalstat", "educat", "homestat", "employstat", "dvryes", "cabdsl") var.num <- c("agemid", "incmid") #coerce var.factor to factor and var.num to numeric mag1 <- mag %>% dplyr::mutate_at(vars(var.factor), as.factor) %>% dplyr::mutate_at(vars(var.num), as.numeric) #only works on factors results <- dummy_recodes(mag1, drop=TRUE, all=FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.