Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a dummy variable in R?

Tags:

variables

r

So, my data set consists of 15 variables, one of them (sex) has only 2 levels. I want to use it as a dummy variable, but the levels are 1 and 2. How do I do this? I want to have levels 0 and 1, but I don't know how to manage this in R!

like image 797
lisa Avatar asked Oct 11 '12 15:10

lisa


People also ask

How do you create a dummy variable?

There are two steps to successfully set up dummy variables in a multiple regression: (1) create dummy variables that represent the categories of your categorical independent variable; and (2) enter values into these dummy variables – known as dummy coding – to represent the categories of the categorical independent ...

How do I convert categorical data to dummy variables in R?

To convert category variables to dummy variables in tidyverse, use the spread() method. To do so, use the spread() function with three arguments: key, which is the column to convert into categorical values, in this case, “Reporting Airline”; value, which is the value you want to set the key to (in this case “dummy”);

What is a dummy variable in R?

Dummy variable in R programming is a type of variable that represents a characteristic of an experiment. A dummy variable is either 1 or 0 and 1 can be represented as either True or False and 0 can be represented as False or True depending upon the user.


2 Answers

With most of R's modelling tools with a formula interface you don't need to create dummy variables, the underlying code that handles and interprets the formula will do this for you. If you want a dummy variable for some other reason then there are several options. The easiest (IMHO) is to use model.matrix():

set.seed(1)
dat <- data.frame(sex = sample(c("male","female"), 10, replace = TRUE))

model.matrix( ~ sex - 1, data = dat)

which gives:

> dummy <- model.matrix( ~ sex - 1, data = dat)
> dummy
   sexfemale sexmale
1          0       1
2          0       1
3          1       0
4          1       0
5          0       1
6          1       0
7          1       0
8          1       0
9          1       0
10         0       1
attr(,"assign")
[1] 1 1
attr(,"contrasts")
attr(,"contrasts")$sex
[1] "contr.treatment"

> dummy[,1]
 1  2  3  4  5  6  7  8  9 10 
 0  0  1  1  0  1  1  1  1  0

You can use either column of dummy as a numeric dummy variable; choose whichever column you want to be the 1-based level. dummy[,1] chooses 1 as representing the female class and dummy[,2] the male class.

Cast this as a factor if you want it to be interpreted as a categorical object:

> factor(dummy[, 1])
 1  2  3  4  5  6  7  8  9 10 
 0  0  1  1  0  1  1  1  1  0 
Levels: 0 1

But that is defeating the object of factor; what is 0 again?

like image 66
Gavin Simpson Avatar answered Oct 11 '22 07:10

Gavin Simpson


Ty this

set.seed(001) # generating some data
sex <- factor(sample(1:2, 10, replace=TRUE)) # this is what you have
[1] 1 1 2 2 1 2 2 2 2 1
Levels: 1 2

sex<-factor(ifelse(as.numeric(sex)==2, 1,0)) # this is what you want
sex  
 [1] 0 0 1 1 0 1 1 1 1 0
Levels: 0 1

If you want labels to be 0 = Male and 1 = Female, then...

sex<-factor(ifelse(as.numeric(sex)==2, 1,0), labels=c('M', 'F')) 
sex # this is what you want
[1] M M F F M F F F F M
Levels: M F

Actually you don't need to create a dummy variable in order to estimate a model using lm, let's see this example:

set.seed(001) # Generating some data
N <- 100
x <- rnorm(N, 50, 20)
y <- 20 + 3.5*x + rnorm(N)
sex <- factor(sample(1:2, N, replace=TRUE))

# Estimating the linear model 
lm(y ~ x + sex) # using the first category as the baseline (this means sex==1)

Call:
    lm(formula = y ~ x + sex)

Coefficients:
(Intercept)            x         sex2  
   19.97815      3.49994     -0.02719     


# renaming the categories and labelling them
sex<-factor(ifelse(as.numeric(sex)==2, 1,0), labels=c('M', 'F'))
lm(y ~ x + sex)  # the same results, baseline is 'Male'

Call:
lm(formula = y ~ x + sex)

Coefficients:
(Intercept)            x         sexF  
   19.97815      3.49994     -0.02719 

As you can see R deals with the dummies pretty well, you just pass them into the formula as factor variable and R will do the rest for you.

By the way there's no need to change the categories from c(2,1) into c(0,1), the results will be the same as you can seen in the example above.

like image 26
Jilber Urbina Avatar answered Oct 11 '22 07:10

Jilber Urbina