我想在数据框中找到一系列值的 IQR。这些值也被分组,因此我需要在数据框中找到每个组的 IQR。我有下表:

 Block DNAname  Spot_Size   Molarity    Cy3_Fluorescence
 1  DNA 01  100pl   100 14266
 1  DNA 01  100pl   100 16020
 1  DNA 01  100pl   100 15705
 1  DNA 01  100pl   100 15783
 1  DNA 01  100pl   100 15834
 1  DNA 01  100pl   50  12248
 1  DNA 01  100pl   50  12209
 1  DNA 01  100pl   50  12511
 1  DNA 01  100pl   50  12316
 1  DNA 01  100pl   50  12469
 1  DNA 01  100pl   25  9626
 1  DNA 01  100pl   25  9804
 1  DNA 01  100pl   25  9794
 1  DNA 01  100pl   25  10020
 1  DNA 01  100pl   25  9739
 1  DNA 01  100pl   10  7158
 1  DNA 01  100pl   10  6802
 1  DNA 01  100pl   10  7378
 1   DNA 01 100pl   10  5949
 1  DNA 01  100pl   10  7484
 1  DNA 01  100pl   5   5257
 1  DNA 01  100pl   5   5560
 1  DNA 01  100pl   5   6076
 1  DNA 01  100pl   5   5925

我运行以下代码来查找 IQR:
aggregate(Cy3.DNA1.100pl.1uM$Cy3_Fluorescence, list(Molarity=
    Cy3.DNA1.100pl.1uM$Molarity, Spot_Size=Cy3.DNA1.100pl.1uM$Spot_Size ), IQR)

这给了我输出:
   Molarity  Spot_Size   x
      5     100pl    384
     10     100pl    576
     25     100pl     65
     50     100pl    221
    100     100pl    129

此输出正确地对所有摩尔浓度进行分组,但 IQR 不正确。如果上面的代码将均值作为函数而不是 IQR,则 x(函数值)的值是正确的:
   Molarity Spot_Size       x
    5     100pl       5752.4
   10     100pl       6954.2
   25     100pl       9796.6
   50     100pl      12350.6
  100     100pl      15521.6

预期的 IQRS 应如下所示:
Molarity IQR
100      324.25
50       258
25       363
10       519.5
5        400

任何帮助将非常感激。如果有人对我如何为 IQR 执行此功能有任何想法,当有一组光斑尺寸(光斑尺寸范围为 100pl-400pl)包括摩尔浓度类别时,我想听到它们。

谢谢你。

最佳答案

目前尚不清楚您的问题是关于聚合,还是关于您的(??)IQR 定义。有多种计算 IQR 的方法(参见 thisthis )。据我所知,它们都没有在您的帖子中产生结果。

至于基于光斑大小和摩尔浓度的聚合,这里有两种方法:

# use aggregate(...) in base R - will be slow with large datasets
aggregate(Cy3_Fluorescence~Molarity+Spot_Size,df,IQR)
#   Molarity Spot_Size Cy3_Fluorescence
# 1        5     100pl            478.5
# 2       10     100pl            576.0
# 3       25     100pl             65.0
# 4       50     100pl            221.0
# 5      100     100pl            129.0

# use data.table - will be extremely fast.
library(data.table)
setDT(df)[,list(IQR=IQR(Cy3_Fluorescence)),by=list(Molarity,Spot_Size)]
#    Molarity Spot_Size   IQR
# 1:      100     100pl 129.0
# 2:       50     100pl 221.0
# 3:       25     100pl  65.0
# 4:       10     100pl 576.0
# 5:        5     100pl 478.5

关于r - 查找行组的 IQR,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27230500/

10-13 01:12