r-在ddply中使用summary根据一列的max()获取整行

  • 本文关键字:一列 max 获取 summary ddply r plyr
  • 更新时间 :
  • 英文 :


df1

  primer timepoints        mean          sde
    Acan          0   1.0000000 0.000000e+00
    Acan         20   0.8758265 7.856192e-02
    Acan         40   1.0575400 4.680159e-02
    Acan         60   1.2399106 2.238616e-01
    Acan        120   1.1710685 2.085558e-02
    Acan        240   1.6430670           NA
    Acan        360   1.7747940           NA

我想要的只是平均值的最大值(对于这些时间点中的任何一个),以及相应的sde。

   ## this will only get me the mean obviously 
   x <- ddply(x, .(primer), summarize, max = max(mean)) 
 primer        max
   Acan   1.774794

## if I were to do this I would obviously not have just the maximum values 
   x <- ddply(x. .(primer,sde), summarize, max = max(mean))

我的一个想法可能是在df中包括时间点,然后匹配两个数据帧以获得一列sde。然后cbind将其绑定到df w/only意味着。

但我觉得有一种更简单的方法可以做到这一点,w/ddply

如果不必使用summary:

ddply(x, .(primer), function(DF) DF[DF$mean == max(DF$mean),]) 

最新更新