一个因素的水平与一个因素的比较

时间:2015-03-10 21:29:53

标签: r comparison

对于模糊的标题感到抱歉,但我无法弄清楚应如何调用此操作。我想这可能是一个留一个过程,但我会解释。我有这个:

structure(list(Ident = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("A_1_2", "A_1_4"), class = "factor"), 
    iduni = c("A_1_2_231", "A_1_2_233", "A_1_2_234", "A_1_2_235", 
    "A_1_2_236", "A_1_2_237", "A_1_4_200", "A_1_4_201", "A_1_4_202", 
    "A_1_4_203", "A_1_4_204", "A_1_4_205", "A_1_4_206"), Dhp = structure(c(1L, 
    3L, 13L, 7L, 11L, 12L, 8L, 9L, 10L, 2L, 6L, 4L, 5L), .Label = c("92", 
    "100", "102", "118", "126", "139", "155", "176", "196", "220", 
    "234", "241", "263"), class = "factor"), ratio = c(NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c("Ident", 
"iduni", "score", "ratio"), row.names = c(1L, 3L, 4L, 5L, 6L, 7L, 
70L, 71L, 72L, 73L, 74L, 75L, 76L), class = "data.frame")

这基本上就是这个小df:

   Ident     iduni score ratio
1  A_1_2 A_1_2_231  92    NA
3  A_1_2 A_1_2_233 102    NA
4  A_1_2 A_1_2_234 263    NA
5  A_1_2 A_1_2_235 155    NA
6  A_1_2 A_1_2_236 234    NA
7  A_1_2 A_1_2_237 241    NA
70 A_1_4 A_1_4_200 176    NA
71 A_1_4 A_1_4_201 196    NA
72 A_1_4 A_1_4_202 220    NA
73 A_1_4 A_1_4_203 100    NA
74 A_1_4 A_1_4_204 139    NA
75 A_1_4 A_1_4_205 118    NA
76 A_1_4 A_1_4_206 126    NA

我希望能够为特定级别的“Ident”选择一个观察值,并将其得分与该级别内的其他每个观察值进行比较。例如,我想获得一个分数与所有其他分数之间的比率之和:

  <=> i =Σ的比率[SCORE(第i行)/ SCORE(第1行:i-1,i + 1:n)]

甚至比较简单:

  

比较i =Σ[SCORE(第1行:i-1,i + 1:n)]

理想情况下,我必须能够在选择其他观察时设置条件(例如,仅针对得分优于得分i)。

先谢谢你的帮助,并对这个问题的尴尬方式表示抱歉。

3 个答案:

答案 0 :(得分:2)

您可以使用data.table。

library(data.table)    
df$score=as.numeric(as.character(df$score))
df <- as.data.table(df)[, ratio1:=score/(sum(score)-score) , by = Ident]
df
    Ident     iduni score ratio     ratio1
 1: A_1_2 A_1_2_231    92    NA 0.09246231
 2: A_1_2 A_1_2_233   102    NA 0.10355330
 3: A_1_2 A_1_2_234   263    NA 0.31917476
 4: A_1_2 A_1_2_235   155    NA 0.16630901
 5: A_1_2 A_1_2_236   234    NA 0.27432591
 6: A_1_2 A_1_2_237   241    NA 0.28486998
 7: A_1_4 A_1_4_200   176    NA 0.19577308
 8: A_1_4 A_1_4_201   196    NA 0.22298066
 9: A_1_4 A_1_4_202   220    NA 0.25730994
10: A_1_4 A_1_4_203   100    NA 0.10256410
11: A_1_4 A_1_4_204   139    NA 0.14850427
12: A_1_4 A_1_4_205   118    NA 0.12330199
13: A_1_4 A_1_4_206   126    NA 0.13277134

答案 1 :(得分:0)

如果您为data.frame df命名,那么这就是您想要的:

df$ratios = ave(df$score,# the variable to summarize
       df$Ident,# the grouping variable(s)
       FUN=function(x)# the function that performs calculations on `df$score` within each grouping.
           # score ratio = this score / (sum of all scores but this one)
           x / (sum(x) - x)
        )


df$comparison_values = ave(df$score,# the variable to summarize
       df$Ident,# the grouping variable(s)
       FUN=function(x)# the function that performs calculations on `df$score` within each grouping.
           # comparison for I = (sum of all scores but this one)
           (sum(x) - x)
        )

答案 2 :(得分:0)

因此,让我们得到每个分数与Ident-column特定值内第3项以上位置的分数平均值的比率。可以使用其他标准,但你不清楚这一点。

#First need to convert the factor to a numeric.
dat$score <- as.numeric(as.character(dat$score))
i=3 # Pick a location 
dat$rat_scr <- ave( dat$score, dat$Ident, FUN=function(x){
                                   x/mean(x[(1+i):length(x) ]) })
 dat
   Ident     iduni score ratio   rat_scr
1  A_1_2 A_1_2_231    92    NA 0.4380952
3  A_1_2 A_1_2_233   102    NA 0.4857143
4  A_1_2 A_1_2_234   263    NA 1.2523810
5  A_1_2 A_1_2_235   155    NA 0.7380952
6  A_1_2 A_1_2_236   234    NA 1.1142857
7  A_1_2 A_1_2_237   241    NA 1.1476190
70 A_1_4 A_1_4_200   176    NA 1.4575569
71 A_1_4 A_1_4_201   196    NA 1.6231884
72 A_1_4 A_1_4_202   220    NA 1.8219462
73 A_1_4 A_1_4_203   100    NA 0.8281573
74 A_1_4 A_1_4_204   139    NA 1.1511387
75 A_1_4 A_1_4_205   118    NA 0.9772257
76 A_1_4 A_1_4_206   126    NA 1.0434783
相关问题