值与 R 中的级别不匹配
values don't match level in R
给定数据框的几列是因子级向量,以其中之一为例:
[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以下的值很多,如上表所示。
如何解决此问题,并让级别自动匹配所有值?谢谢。
您可以通过重新factor
-ing
将顺序更改为数字顺序
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
给定数据框的几列是因子级向量,以其中之一为例:
[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以下的值很多,如上表所示。
如何解决此问题,并让级别自动匹配所有值?谢谢。
您可以通过重新factor
-ing
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