Creating Dummy Variables in R

categorical
dummy_variable

#1

How can we create dummy variables in R? Especially, when we have categorical variables having more than 2 levels.


#2

Lets say you have 3 levels (child, female, male) in a column named “col” of your dataset x.

#Create new column: dummy

dummy <- NULL
dummy[x$col == “child”] = 0
dummy[x$col == “female”] = 1
dummy[x$col == “male”] = 2


#3

Use dummies package in R,
Name all the variables which are Categorical.
dummy.data.frame(data, variables)

You will get the data set with dummy variables


#4

@Chaitanya333. Thank you. But at times we might have to retain certain categorical variables. For example, level of education. It will be wise to have the variable to have levels converted to 1,2,3,4 instead of creating 4 dummy variables with 0,1 values.
So, will the function takes care of such variables?


#5

@Shivraj_Mohite Thank you. Is there any shortcut as this will be tedious when we have more than 10 such variables?


#6

I think there is a way to do this using Transform() or even using data.table library- Using setDT function. Google and you should be able to retro fit for your need.