我有一个因子列,我得到了一个缺失的级别,为什么R创建了这个缺失级别?
d0s$y
[1] E E E E E E G G G G G G G P P P P P P P
Levels: E G P
levels(d0s$y)
[1] "" "E" "G" "P"
答案 0 :(得分:1)
可能是在子集化之前数据集中存在空白""
。一种方法是调用droplevels
删除未使用的级别
d0s$y <- droplevels(d0s$y)
或再次致电factor
d0s$y <- factor(d0s$y)
但是,也可能是""
元素已经存在,但因为它是factor
打印选项不显示
y1 <- factor(rep(c("E", "G", "P", ""), each = 3))
y1
#[1] E E E G G G P P P
#Levels: E G P
levels(y1)
#[1] "" "E" "G" "P"
假设我们将'y1'
分组 y2 <- y1[y1 %in% c("E", "G", "P")]
levels(y2) #the unused levels are still there
#[1] "" "E" "G" "P"
除非我们放弃这些级别
levels(droplevels(y2))
#[1] "E" "G" "P"