to.dummy: Convert categorical vector into dummy binary dataframe

View source: R/to.dummy.R

to.dummyR Documentation

Convert categorical vector into dummy binary dataframe

Description

This function gets a vector that contains some categories and convert it to dummy columns (also known as binary columns). The number of output columns is equal to the input categories.

Usage

    to.dummy(v, prefix)

Arguments

v

A character, numeric or factor vector that contains the categories. (Mandatory)

prefix

A character string to attach to the beginning of the column names to prevent confusion or conflicts. (Mandatory)

Details

This function simplifies the procedure of making data ready for those learning algorithms or methods that cannot handle categorical columns. It works by getting a character, numeric or factor vector and convert it to some columns that each of which represent a category from the input vector. For example a vector of eye color with different categories like Black, Brown, Blue, Green will be transformed into a dataframe with 4 columns and each column has value of 1 for samples that have that specific eye color.

Value

A data.frame is returned which only contains 0 and 1 as values. Number of this data.frame columns is equal to number of categories in the original input vector.

Author(s)

Mehrad Mahmoudian

Examples

    # load a dataframe (from base package)
    data(iris)
    
    # see the actual values of the categorical column
    print(iris$Species)
    
    # convert to dummy
    binary_species <- to.dummy(iris$Species, "species")
    # view the first few lines of the binary_species data.frame
    head(binary_species)

varhandle documentation built on Oct. 1, 2023, 1:08 a.m.