Possible duplicate:
Maintain a minimum value for each factor level
Here is my problem, I want to select rows with the minimum value in the specified column. For instance:
df <- data.frame(A=c("a","a","b","b"),value=1:4)
As a result i want
A value a 1 b 3
I could do with by and ddply , but they are pretty slow when df is huge and has many different meanings in A
do.call(rbind,by(df,df$A, function(x) x[which.min(abs(x$value)),],simplify=FALSE)) ddply(df, ~A, function(x){x[which.min(abs(x$value)),]})
Any suggestions?
Thanks a lot!
source share