我有一个数据集,该数据集在一列中具有多个分类值,但似乎找不到找到将它们转换为数字的方法。
区域:标识销售的一般区域分类。
A Agriculture
C Commercial
FV Floating Village Residential
I Industrial
RH Residential High Density
RL Residential Low Density
RP Residential Low Density Park
RM Residential Medium Density
这是我的代码:
data$Zone <- as.numeric(factor(data$Zone))
完成此操作后,R会将值更改为1,2,3,4,5等。 如果是这样,我怎么知道哪个数字是指原始分类值?
答案 0 :(得分:0)
factor(data$Zone)
时,R默认情况下根据字母顺序设置级别。
但是您可以更改它或为了安全起见,通过以下方式指定级别
data$Zone <- factor(data$Zone, levels = c("A", "C", "I", "FV", ...))
然后,当您as.numeric()
data$Zone
时,A为1,C为2,我为3,FV为4,依此类推。