我有一个包含79个解释变量的数据集,其中43个是因子。
一些因子变量只是泛型标签-对于那些我打算使用虚拟变量来表示数字的变量。
因子变量的其他一些子集包含有序级别,例如:
BsmtQual: Evaluates the height of the basement
Ex Excellent (100+ inches)
Gd Good (90-99 inches)
TA Typical (80-89 inches)
Fa Fair (70-79 inches)
Po Poor (<70 inches
NA No Basement我希望将这样的因子变量转换为一个数值,该数值将保持级别从最低到最高的顺序,这意味着在操作之后,我希望得到如下内容:
BsmtQual: Evaluates the height of the basement
Ex records will be replaced with: 6
Gd records will be replaced with: 5
TA records will be replaced with: 4
Fa records will be replaced with: 3
Po records will be replaced with: 2
NA records will be replaced with: 1(请注意,我是否可以用0替换NA -因为NA实际上并不是指此变量的缺失数据,而是指具有较低基分数的记录)
如何对这种替代方案进行编码?
发布于 2018-05-04 11:48:26
req_var$ExterQual <- revalue(req_var$ExterQual, c("Ex"=5 ,"Gd"=4 , "TA"=3 , "Fa"=2 ,"Po"=1)) 在这里,我将不考虑这些数据集中的NA。如果您想将数字NA指定为0,则在上面的命令中添加"NA"=0。
https://stackoverflow.com/questions/42457548
复制相似问题