[R] Seeking to Dummify Categorical Variables

BR_email br at dmstat1.com
Sun Apr 2 20:48:00 CEST 2017

Hi R'ers:
I need a jump start to obtain my objective.
Assistance is greatly appreciated.

#Given Gender Dataset
r1       <- c( 1, 2, 3)
c1       <- c( "male", "female", "NA")
GENDER <- data.frame(r1,c1)
names(d1_3) <- c("ID","Gender")
i.e., to generate two new numeric columns,
         Gender_male and Gender_female,
such that:
when Gender="male"   then Gender_male=1 and Gender_female=0
when Gender="female" then Gender_male=0 and Gender_female=1
when Gender="NA"     then Gender_male=0 and Gender_female=0

So, with the given dataset, the resultant dataset would be as follows:
Desired Extended Gender Dataset
ID Gender Gender_male Gender_female
1      male              1                   0
2   female              0                   1
3       NA               0                   0

Bruce Ratner, Ph.D.

More information about the R-help mailing list