Hadsga
Hadsga

Reputation: 333

Value label for variable

I want to attach a value label to the variables (0 = "Male" and 1 = "Female") in my data frame.

I tried this:

CB$gender <- factor(CB$gender, levels = c(0,1), labels = c("Male", "Female"))

But I get this result:

[1]  Female Male   Male   Male   Female Female Male   Female Female Male   
     Male   Male   Male   Male  
[15] Male   Female Female Male   Female Female Male   Male   Male   Female 
     Male   Female Male   Male  
[29] Male   Female Female Male

Any Idea?

Upvotes: 1

Views: 609

Answers (1)

Chabo
Chabo

Reputation: 3000

I don't quite know what is wrong but splitting it apart like this seemed to work

 gender<-c("Male","Female","Male","Female")

 gender <- as.factor(gender)

 levels(gender)<-c(0,1)

Of course, pay attention to the fact I used my own made up data.

So try to subset the data first

gender<-CB$gender    

And then apply the above. Best of luck!

Upvotes: 1

Related Questions