how to change gender factor into an numerical coding in r

Saraya picture Saraya · Oct 11, 2013 · Viewed 13.4k times · Source

I have a factor of males and females say c("male", "female","female") and I want to create a vector of c(0,1,1) How can i change that in r?

Answer

droopy picture droopy · Oct 11, 2013

With boolean :

a <- c("male", "female","female")
(a=="female")*1

hth