Converting a factor with 2 levels to binary values 0/1 in R

user2093989 picture user2093989 · Feb 21, 2013 · Viewed 50.3k times · Source

I have a variable, called gender, with binary categorical values "female"/"male". I want to change its type to integers 0/1 so that I can use it in a regression analysis. i.e I want values "female" and "male" to be mapped to 1 and 0.

> str(gender)
gender : Factor w/ 2 levels "female","male":  1 1 1 0 0 0 0 1 1 0 ...
> gender[1]
[1] female

I would like to convert gender variable type so that I get int value 1 when I query an element, i.e.

> gender[1]
[1] 1

Answer

thelatemail picture thelatemail · Feb 21, 2013

As an addition to @Dason's answer, note that...

test <- c("male","female")

as.factor(test)
#[1] male   female
#Levels: female male

...will return female as the reference group (1) and male as the comparison group (2),

To spin it the other way, you would need to do...

factor(test,levels=c("male","female"))
#[1] male   female
#Levels: male female

As @marius notes, using contrasts will show you how it will work in the regression model:

contrasts(as.factor(test))
#       male
#female    0
#male      1

contrasts(factor(test,levels=c("male","female")))
#       female
#male        0
#female      1