I thought I perfectly understood the factor class in R, but finally, I may not. I can't find a way to transform a vector of 0/1 into a factor without transforming the original value in 1/2. Here is a minimal working example:
foo<-c(1,0,0,1,1,1,0,NA,0,1)
bar<-factor(foo)
str(bar)
results in:
Factor w/ 2 levels "0","1": 2 1 1 2 2 2 1 NA 1 2
and
bar<-factor(foo, labels = c("(0) No", "(1) Yes"))
str(bar)
results in:
Factor w/ 2 levels "(0) No","(1) Yes": 2 1 1 2 2 2 1 NA 1 2
and
bar<-factor(foo, levels= c(0,1), labels = c("(0) No", "(1) Yes"))
str(bar)
results in:
Factor w/ 2 levels "(0) No","(1) Yes": 2 1 1 2 2 2 1 NA 1 2
Is it because R can have a level equal to 0?
I really want to keep the original values as 0/1 to facilitate data sharing, but could someone provide a solution?