lisa
lisa

Reputation: 660

How do I make a dummy variable in 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!

Upvotes: 9

Views: 54226

Answers (3)

Vishal Lala
Vishal Lala

Reputation: 49

As suggested by many above, turn it into factor.

If you really want to dummy code the gender variable, consider this

set.seed(100)
gender = rbinom(100,1,0.5)+1
gender_dummy = gender-1

Upvotes: 1

Jilber Urbina
Jilber Urbina

Reputation: 61154

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.

Upvotes: 9

Gavin Simpson
Gavin Simpson

Reputation: 174778

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?

Upvotes: 25

Related Questions