2014-01-09 106 views
1

好吧7級因子水平的5,這可能是那麼容易,沒有人寫它(或者我找不到它):情節只有R中

我有7個因素變量:

[1] "NICHT ERHOBEN"  "STIMME VOLL ZU"  "STIMME EHER ZU"  "STIMME EHER NICHT ZU" 
[5] "STIMME GAR NICHT ZU" "WEISS NICHT"   "KEINE ANGABE" 

我想用plot(df $ v1,df $ v_with_factors)來繪製變量,而R很好。

繪圖時如何省略第一個(「nicht erhoben」)和最後一個(「keine angabe」)因子?

回答

3

是的,它是一個煩人的尷尬卻相當常見的任務 - 但是你可以使用droplevels

> DF= data.frame(nums=1:8, facs=as.factor(c("A", "A", "B", "B", "C", "C", "D", "D"))) 
    nums facs 
1 1 A 
2 2 A 
3 3 B 
4 4 B 
5 5 C 
6 6 C 
7 7 D 
8 8 D 

繼承人的方式與空格:

> with(DF[which(DF$facs!="A"),], plot(facs, nums)) 

enter image description here

> with(droplevels(DF[which(DF$facs!="A"),]), plot(facs, nums)) 

這是你以後droplevels

enter image description here

+0

工程就像一個魅力,謝謝! :) – LukasKawerau

+0

不客氣。 –

1

做你的數據子集繪製之前:子數據< - 子集(DF $ V1,條件)

1

不知道你的數據究竟是如何模樣的解決方案很可能是下面的(我用不上至R就在此刻進行測試,但這種方法應該是罰款):

# Indices of all levels of the factor that you want to plot 
lindex = which(!(levels(df$v_with_factors) %in% c("NICHT ERHOBEN", "KEINE ANGABE"))) 
# Indices of all data records that correspond to the selected levels of the factor 
index = [email protected] %in% lindex 

plot(df$v1[index], df$v_with_factors[index]) 
+0

這工作,謝謝!斯蒂芬的版本工作更好一點(仍然打印yaxlabels),所以我接受他的答案,但你的工作也是如此:)再次感謝! – LukasKawerau