to.dummy | R Documentation |
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.
to.dummy(v, prefix)
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) |
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.
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.
Mehrad Mahmoudian
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.