dummify: Make Dummy (Binary) Variable from the Values of Another...

Description Usage Arguments Value Examples

View source: R/dummify.R

Description

This function allows you to construct a series of dummy variables from the values of a separate variable. E.g., if you have a variable called "race" with the following values: "White","Black","Asian","Native American/Alaskan Native", "Native Hawaiian", and "Other"–this function creates binary variables for each of the values, or however many the user specifies.

Usage

1
dummify(data, var, reference = NULL, dumNames = NULL, keepNA = TRUE)

Arguments

data

A dataframe containing the variable to be dummied.

var

A string naming the variable to be dummied or the column index. Accepts variables integer, string, and factor values.

reference

An optional parameter that allows users to set and/or identify reference values with either TRUE, a character list, or a named list. Defaults to NULL.If TRUE, dummify() will leave out the first value encountered in the variable. If reference is a single variable, dummify() will leave out that variable. If reference is multiple variables, then dummify() will leave out all identified variables.

dumNames

An optional parameter that allows users to name the dummy variables they create with dummify(). Accepts a character vector or a named list. If passed a character vector, the columns will be renamed in the order of the list. If reference is a named list, dummify() will rename the dummied column where the named value is equal to 1 with the name passed.

keepNA

Defaults to TRUE. An optional parameter that allows users to decide if they want to consider NAs as values to be turned into a dummy column.

Value

The inputted dataframe with additional columns containing the dummified variables

Examples

1
2
3
4
5
6
7
8
9
dummify(data = iris, var = "Species")
dummify(data = iris, var = 5)
dummify(data = iris, var = "Species", reference = TRUE)
dummify(data = iris, var = "Species", reference = "setosa")
dummify(data = iris, var = "Species", reference = "setosa", keepNA = FALSE)
dummify(data = iris, var = "Species", reference = c("setosa","virginica"))
dummify(data = iris, var = "Species", dumNames = c("BristlePointed","BlueFlag","Virginia"))
dummify(data = iris, var = "Species", dumNames = c("BristlePointed" = "setosa",
"BlueFlag" = "versicolor","Virginia" = "virginica"))

prlitics/dumdum documentation built on Aug. 12, 2020, 12:54 a.m.