[R] data frame select max group by like function

Henrique Dallazuanna wwwhsd at gmail.com
Tue Mar 9 19:58:26 CET 2010


Try this:

library(sqldf)
sqldf("SELECT ID, Year, MAX(score) FROM DF GROUP BY ID")

Or

do.call(rbind, lapply(split(DF <- DF[order(DF$score),], DF$ID), tail, 1))

On Tue, Mar 9, 2010 at 3:35 PM, Tan, Richard <RTan at panagora.com> wrote:
> Hi, I have a data frame with 3 columns: ID, year and score.  How can I
> select for each unique ID, the year that has the max score?  For
> example, for data frame
>
> ID, year, score
> tom, 1995, 88
> rick, 1994, 90
> mary, 2000, 97
> tom, 1998, 60
> mary, 1998,100
>
> I shall have
> ID, year, score
> tom, 1995, 88
> rick, 1994, 90
> mary, 1998,100
>
> Thanks,
> Richard
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list