You can use the plyr package for plyr . For example, if your data is:
d <- data.frame(id.a=c(1,1,1,2,2,3,3,3,3), id.b=c(1,2,3,1,2,1,2,3,4), dist=c(12,10,15,20,18,16,17,25,9)) id.a id.b dist 1 1 1 12 2 1 2 10 3 1 3 15 4 2 1 20 5 2 2 18 6 3 1 16 7 3 2 17 8 3 3 25 9 3 4 9
You can use the ddply function as follows:
library(plyr) ddply(d, "id.a", function(df) return(df[df$dist==min(df$dist),]))
What gives:
id.a id.b dist 1 1 2 10 2 2 2 18 3 3 4 9
juba
source share