You may be able to help me: For each ID, I wish to extract the largest "a" value that has the largest "b" value. In other words, I wish to scan through the "b" values, identify the highest (here b=40). If several "a" have the same highest "b" value (here a=20 and a=30), then I wish to select the highest "a" value (here a=30).
Here is what I have done so far:
df<- data.frame(ID=c('1','1','1','1','1','1'), a=c('10','20','30','10','2','30'), b=c('10','20','30','10','40', "40")) library(plyr) opt <- ddply(df,.(ID),summarise, a=a[which.max(b)]) opt ID a 1 2 but, I don't get:
ID a 1 30 I'd greatly appreciate your suggestions. Note that contrary to this sample dataset, the actual dataset I work on is pretty large. Thank you very much!
which.maxgives you the index. For the value just usemax.