我想在我的数据框中为变量(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
任何想法?
答案 0 :(得分:1)
我不知道出了什么问题,但是将它拆分开来似乎有效
gender<-c("Male","Female","Male","Female")
gender <- as.factor(gender)
levels(gender)<-c(0,1)
当然,请注意我使用自己编造的数据的事实。
因此,首先尝试对数据进行子集化
gender<-CB$gender
然后应用上述内容。祝你好运!