变量的值标签

时间:2017-08-14 15:22:43

标签: r r-factor

我想在我的数据框中为变量(0 =“Male”和1 =“Female”)附加一个值标签。

我试过了:

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

但我得到了这个结果:

[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

任何想法?

1 个答案:

答案 0 :(得分:1)

我不知道出了什么问题,但是将它拆分开来似乎有效

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

 gender <- as.factor(gender)

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

当然,请注意我使用自己编造的数据的事实。

因此,首先尝试对数据进行子集化

gender<-CB$gender    

然后应用上述内容。祝你好运!