using summarize in ddply to get entire row based on max() of one column

若如初见. 提交于 2019-12-10 23:31:31

问题


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

all I want is the max value of mean (for any of these timepoints) w/ it's corresponding 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))

one idea I had would be to may be to include timepoints in the df then match the two data frames to get a column of sdes. Then cbind that to the df w/ only means.

But I feel as if there is an easier way to do this w/ ddply


回答1:


If you don't have to use summarise:

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


来源:https://stackoverflow.com/questions/12063527/using-summarize-in-ddply-to-get-entire-row-based-on-max-of-one-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!