2013-10-21 36 views
1

用戶,如何避免情節中的空因素水平?

我有這樣的數據幀:

A<- c(10,2,4,5,3,5,98,65,36,65,6,100,70,54,25,23,22,30,15,23) 
B<- c(1,0.1,0.5,0.8,0.2,0.9,3,1.2,5.6,3.5,15.9,10.2,5,5.1,7.1,5,6,10,4,8) 
C<- c("a","a","a","a","a","a","b","b","b","b","c","c","c","c","d","d","d","d","d","d") 
mydf<- data.frame(A,B,C) 

,我做了一個子集,僅保留 「A」 級。

subset<- subset(mydf, mydf$C=="a") 

但是當我做一個情節(請看圖像)時,圖表也顯示了刪除的級別。

plot(B~ C, data=subset) 

如何繪製避免刪除級別的子集數據框?

plot subset data

謝謝!

回答

3
str(subset) 
#'data.frame': 6 obs. of 3 variables: 
# $ A: num 10 2 4 5 3 5 
# $ B: num 1 0.1 0.5 0.8 0.2 0.9 
# $ C: Factor w/ 4 levels "a","b","c","d": 1 1 1 1 1 1 

通過factor手段刪除丟失因子水平:

subset$C <- factor(subset$C) 

str(subset) 
#'data.frame': 6 obs. of 3 variables: 
#$ A: num 10 2 4 5 3 5 
#$ B: num 1 0.1 0.5 0.8 0.2 0.9 
#$ C: Factor w/ 1 level "a": 1 1 1 1 1 1 
+0

太容易了!謝謝你JT85 – FraNut

1

只要做到:

plot(B~ droplevels(C), data=subset) 
2

使用droplevels

subset$C <- droplevels(subset$C) 
plot(B~ C, data=subset) 

順便說一句,subset是不是一個data.frame一個好名字。

+0

我同意,我想改變的子集的名字,但答案來更快然後我的編輯。謝謝Jilber – FraNut