[R] Recoding categorical gender variable into numeric factors

Conradsb csbaldne at vt.edu
Wed Sep 5 21:14:06 CEST 2012


I currently have a data set in which gender is inputed as "Male" and "Female"
, and I'm trying to convert this into "1" and "0".

I found a website which reccomended using two commands:

data$scode[data$sex=="M"] <- "1"
data$scode[data$sex=="F"] <- "2"

to convert to numbers, and:

data$scode <- factor(data$scode)

to convert this variable to a factor.



My issue is that, after I use the first command, *only* the female values
get converted to a number. I am left with a column filled with 2's and blank
spaces. Instead of typing both lines of the first command, I copy and pasted
the first line and changed the letter representing gender. I also made sure
that both letters were exactly as they appear in the dataset.

My questions are: is there any visible issue with my syntax, and are there
any other methods to accomplish this?

I'm also very new to R, so complex syntax is beyond me.

Conrad Baldner



--
View this message in context: http://r.789695.n4.nabble.com/Recoding-categorical-gender-variable-into-numeric-factors-tp4642316.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list