dummify_factors: Convert all categorical variables into dummy/indicator...

Description Usage Arguments Value Examples

View source: R/dummify_factors.R

Description

Takes a data frame and separates all columns containing categorical variables into dummy variables (one column per category). Unlike model.matrix(), does not exclude any categories from being converted into columns, and retains missing values.

Usage

1
dummify_factors(df, dummify_characters = TRUE, max_levels = 52, sep = "__")

Arguments

df

A data frame containing the factors to be converted to dummies.

dummify_characters

Should character variables be converted to factors and dummified? Defaults to TRUE.

max_levels

The maximum number of levels that a categorical variable can have in order to be converted to dummies. This is to prevent converting variables such as respondent IDs or open-ended responses to dummy variables. Defaults to 52, which is slightly more than the number of U.S. states plus Washington, D.C.

sep

A character string that will go in between the original variable name and the corresponding factor level in the output, e.g. VARIABLE__Category. Defaults to "__".

Value

A copy of the original data frame where all factor variables have been replaced with dummies. Formatted as a tibble.

Examples

1
2

pewresearch/pewmethods documentation built on March 27, 2020, 7:22 p.m.