2016-09-08 143 views
1

我有一個因子列,我得到一個缺失的等級,爲什麼R會創建這個缺失的等級?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" 

回答

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"