簡體   English   中英

在R中僅繪制7個因子水平中的5個

[英]Plot only 5 of 7 factor levels in 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”)因素?

是的,這是一個令人討厭的尷尬但很常見的任務-但是您可以使用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))

在此處輸入圖片說明

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

這是在您droplevels

在此處輸入圖片說明

在繪制之前,請對數據做一個子集:子數據<-subset(df $ v1,條件)

不知道解決方案的確切數據可能是以下情況(目前我無法訪問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 = df$v_with_factors@.Data %in% lindex

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM