Possible Duplicate:
Only keep min value for each factor level
Here is my problem, I want to select rows with minimum value in specified column. For example:
df <- data.frame(A=c("a","a","b","b"),value=1:4)
The result I want is
A value
a 1
b 3
I could do with by
and ddply
, but they are quite slow when df is huge and has many different value 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!