dataframe - Two equal max values in R -
i have dataframe numbers(score) , repeating id. want maximum value each of id. used function
top = aggregate(df$score, list(df$id),max)
this returned me top dataframe maximum values corresponding each id.
but happens 1 of id, have 2 equal max value. function ignoring second value.
is there way retain both max values.?
for example:
df
id score 1 12 1 15 1 1 1 15 2 23 2 12 2 13
the above function gives me this: top
id score 1 15 2 23
i need this: top
id score 1 15 1 15 2 23
i recommend data.table
chris mentioned (good speed, steeper learning curve). or if don't want data.table
use plyr
:
library(plyr) ddply(df, .(id), subset, score==max(score)) # same ddply(df, .(id), function (x) subset(x, score==max(score)))
Comments
Post a Comment