r 程序将 yes/no 变量更改为 1/0 - "变量“奖牌"不是一个因素"; [英] r program changing yes/no variable to 1/0 - " variable 'medal' is not a factor"
本文介绍了r 程序将 yes/no 变量更改为 1/0 - "变量“奖牌"不是一个因素";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 R 的新手.我有一个分类变量,我喜欢制作线性模型并用它进行预测,但 RStudio 不允许我这样做,除非我更改变量的类型.如何将是/否更改为 1/0?我的错误是变量‘奖牌’不是一个因素"我试过了:
I am new to R. I have a categorical variable that I like to make a linear model and do prediction with it but RStudio does not let me do so unless I change the type of the variable. How can I change the yes/no to 1/0? my error is " variable 'medal' is not a factor" I have tried :
> sport$medal <- factor(sport$medal)
> is.factor(sport$medal)
[1] FALSE
推荐答案
除了明显的错别字...
Apart from the obvious typo...
如何将是/否更改为 0/1?
How can I change the yes/no to 0/1?
你需要
sport$medal <- factor(sport$medal, levels = c("yes", "no"))
默认行为将为您提供 0 代表否"和 1 代表是",因为n"按字母顺序排在y"之前.
The default behaviour will give you 0 for "no" and 1 for "yes", as "n" comes ahead of "y" in alphabetical order.
这篇关于r 程序将 yes/no 变量更改为 1/0 - "变量“奖牌"不是一个因素";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文