Question
I am interested in converting numerical binary information into gender. During this, I came upon some behavior in R that I do not understand.
factor(c(0,1,0,1),labels = c("male","female"))
This works as intended. You get the following output:
[1] male female male female
Levels: male female
However, when I decide to be explicit and type the following:
factor(c(0,1,0,1),levels = c("male","female"), labels = c("male","female"))
It converts the numerical data to NA. This is disturbing to me because I am both specifying the levels and the labels. In my mind, the code I have written is equivalent, but is being interpreted by base R differently.
[1] <NA> <NA> <NA> <NA>
Levels: male female
My question is simple: why?
Caveats
I went to the factor function in R documentation. I have googled this question and searched on stackoverflow and as far as I know, this is so incredible simple that is has either not been asked or I asked it in such a way I could not find a duplicate. Thanks for your understanding.