Converting a factor with 2 levels to binary values 0/1 in R
binary, r, r-factor
Solution
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
Problem
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 ```