给定数据帧的几列是因子水平向量,以其中一个为例:
[4661] 12.7 13.2 10.1 12.3
[4665] Not Available 12.7 Not Available Not Available
[4669] Not Available Not Available Not Available 11.1
[4673] 9.9 9 Not Available 9.7
[4677] 8.9 10.1 11.7 13
[4681] Not Available 11.5 Not Available Not Available
[4685] 12.3 11.2 Not Available 11.1
[4689] 10.4 Not Available Not Available Not Available
[4693] 11.5 Not Available Not Available Not Available
[4697] Not Available Not Available 12 Not Available
[4701] Not Available Not Available Not Available Not Available
[4705] Not Available Not Available
106 Levels: 10 10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 ... Not Available
我觉得很烦人,它一直说106级别从10开始然后是10.1,但事实上有很多值低于10,如上面列表所示。
如何解决这个问题,让级别自动匹配所有值?谢谢。
答案 0 :(得分:1)
您可以使用重新factor
-
x <- factor(c(10,10.1,10.2,9,8,1,"Not available"))
levels(x)
#[1] "1" "10" "10.1" "10.2"
#[5] "8" "9" "Not available"
factor(x, levels=levels(x)[order(as.numeric(as.character(levels(x))),na.last=TRUE)])
#[1] 10 10.1 10.2 9 8 1 Not available
#Levels: 1 8 9 10 10.1 10.2 Not available
可以说你应该首先将这些数据用作数字,例如:
as.numeric(as.character(x))
#[1] 10.0 10.1 10.2 9.0 8.0 1.0 NA