Learn R Programming

hgwrr (version 0.6-2)

make_dummy: Make Dummy Variables

Description

Function make_dummy converts categorical variables in a data frame to dummy variables.

Function make_dummy_extract converts a column to dummy variables if necessary and assign appropriate names. See the "detail" section for further information. Users can define their own functions to allow the model deal with some types of variables properly.

Usage

make_dummy(data)

make_dummy_extract(col, name)

# S3 method for character make_dummy_extract(col, name)

# S3 method for factor make_dummy_extract(col, name)

# S3 method for logical make_dummy_extract(col, name)

# S3 method for default make_dummy_extract(col, name)

Value

The data frame with extracted dummy variables.

Arguments

data

The data frame from which dummy variables need to be extracted.

col

A vector to extract dummy variables.

name

The vector's name.

Details

If col is a character vector, the function will get unique values of its elements and leave out the last one. Then, all the unique values are combined with the name argument as names of new columns.

If col is a factor vector, the function will get its levels and leave out the last one. Then, all level labels are combined with the name argument as names of new columns.

If col is a logical vector, the function will convert it to a numeric vector with value TRUE mapped to 1 and FALSE to 0.

If col is of other types, the default behaviour for extracting dummy variables is just to copy the original value and try to convert it to numeric values.

Examples

Run this code
make_dummy(iris["Species"])

make_dummy_extract(iris$Species, "Species")

make_dummy_extract(c("top", "mid", "low", "mid", "top"), "level")

make_dummy_extract(factor(c("far", "near", "near")), "distance")

make_dummy_extract(c(TRUE, TRUE, FALSE), "sold")

Run the code above in your browser using DataLab