R 中的因子水平未显示为数字

factor level in R not displayed as numeric

我有一个有 200 万行的 data.frame。其中一列是一个字母数字 ID,它在该列中重复出现,唯一计数为 300000?

>head(df$ID)
    ID 
AB00153232de 
AB00153232de    
AB00153232de 
AB00155532gh 
AB00155532gh 
AB00158932ij

>df$ID<-factor(df$ID)

当我尝试打印那个因子变量时,我得到这样的结果:

>df$ID
[1] AB00153232de AB00153232de AB00153232de AB00155532gh AB00155532gh AB00158932ij 
320668 Levels: AB00153232de AB00155532gh AB00158932ij..... 

因子没有存储为数值向量吗?为什么?

改用as.integer(df$ID)

示例:

R> ex <- as.factor(LETTERS)
R> ex
 [1] A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Levels: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
R> str(ex)
 Factor w/ 26 levels "A","B","C","D",..: 1 2 3 4 5 6 7 8 9 10 ...
R> as.integer(ex)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
R> 

对因子变量使用 unclass。它将因子水平保留为新变量的属性,以便将来需要时可以使用它。

df1$ID
#     [1] AB00153232de AB00153232de AB00153232de AB00155532gh AB00155532gh AB00158932ij
# Levels: AB00153232de AB00155532gh AB00158932ij

unclass(df1$ID)
# [1] 1 1 1 2 2 3
# attr(,"levels")
# [1] "AB00153232de" "AB00155532gh" "AB00158932ij"

数据:

df1 <- structure(list(ID = structure(c(1L, 1L, 1L, 2L, 2L, 3L), 
                                     .Label = c("AB00153232de", "AB00155532gh", "AB00158932ij"), class = "factor")), 
                 .Names = "ID", row.names = c(NA, -6L), class = "data.frame")