簡體   English   中英

如何將一個因子水平與R中的所有剩余水平進行比較

[英]How can I compare one level of a factor with all remaining levels in R

我有一個類似於內置InsectSprays的數據框(帶有因子和數字數據),但它包含10個以上數字和20個因子矢量,幾乎沒有NA。 當我的boxplot(數字〜因子)時,我注意到某些級別突出,我希望能夠將它們與其余級別進行比較。

例如:InsectSprays包含一個名為count(0:26)的數字向量,以及一個名為Sprays的因子向量:A,B,C,D,E和F.在InsectSprays中,C是最低的,所以我想要cbe能夠將C與所有其他人進行比較。

我為這樣的數字向量寫了一個函數:

num_interlevel <- function(df, variable, category){
  #find the levels of the categorizing parameter
  level.list <- levels(category)
  #build enough columns in the plot area
  par(mfrow=c(1,length(level.list)))
  for(i in 1:length(level.list)){
    #subset the df containing only the level in question
    variable.df <- na.omit(df[which(category == level.list[i]),])
    #subset the df containing all other levels
    category.df <- na.omit(df[which(category != level.list[i]),])
    boxplot(variable.df[, variable], category.df[, variable])
    p <- t.test(variable.df[, variable], category.df[, variable])$p.value
    title(paste(level.list[i], "=", p))
  }
}

num_interlevel(InsectSprays, "count", InsectSprays$spray)給了我想要的結果。

但是當談到將因子向量相互比較時(我使用表格),它不起作用,僅僅因為數據幀的大小不同,更重要的是,因為這是一種錯誤的方式。

然后我認為可能有一個現有的功能,但找不到任何功能。 任何人都可以建議一種方法/功能來創建一個只包含一個級別的子集和另一個包含所有其他級別的子集嗎?

#dput:
structure(list(Yas = c(27, 18, 17, 18, 18), Cinsiyet = structure(c(2L, 
2L, 2L, 1L, 1L), .Label = c("Erkek", "Kadın"), class = "factor"), 
Ikamet = structure(c(5L, 4L, 3L, 3L, 5L), .Label = c("Aileyle", 
"Akrabayla", "Arkadaşla", "Devlet yurdu", "Diğer", "Özel yurt", 
"Tek başına"), class = "factor"), Aile_birey = c(13, 9, 6, 
10, 6), Aile_gelir = c(700, 1000, 1500, 600, 800)), .Names = c("Yas", 
"Cinsiyet", "Ikamet", "Aile_birey", "Aile_gelir"), row.names = c(NA, 
5L), class = "data.frame")

編輯

詹姆斯回答后我改編了我的職能。 這肯定不是一個優雅的解決方案,但我把它放在這里以供將來參考:

n.compare <- function(df, variable, category){
  level.list <- levels(df[,category])
  par(mfrow=c(1,length(level.list)))
  for(i in 1:length(level.list)){
    boxplot(df[,variable] ~ (df[,category] == level.list[i]))
    p <- t.test(df[,variable] ~ (df[,category] == level.list[i]))$p.value
    title(paste(level.list[i], "=", p))
  }
}

f.compare <- function(df, variable, category){
  level.list <- levels(df[,category])
  par(mfrow=c(1,length(level.list)))
  for(i in 1:length(level.list)){
    print(paste(level.list[i]))
    print(table((df[,category] == level.list[i]), df[,variable]))
    writeLines("\n")
  }
}

要拆分data.frame,請使用split

lapply(split(InsectSprays,InsectSprays$spray=="A"),summary)
$`FALSE`
     count       spray 
 Min.   : 0.00   A: 0  
 1st Qu.: 3.00   B:12  
 Median : 5.00   C:12  
 Mean   : 8.50   D:12  
 3rd Qu.:13.25   E:12  
 Max.   :26.00   F:12  

$`TRUE`
     count       spray 
 Min.   : 7.00   A:12  
 1st Qu.:11.50   B: 0  
 Median :14.00   C: 0  
 Mean   :14.50   D: 0  
 3rd Qu.:17.75   E: 0  
 Max.   :23.00   F: 0  

還要考慮以下事項:

boxplot(count~(spray=="A"),InsectSprays)

暫無
暫無

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

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