[R] data frame select max group by like function
William Dunlap
wdunlap at tibco.com
Tue Mar 9 23:58:17 CET 2010
And yet another way is
> isLastInRun <- function(x)c(x[-1]!=x[-length(x)], TRUE)
> sortedDat <- dat[order(dat$ID,dat$score),]
> sortedDat[isLastInRun(sortedDat$ID),]
ID year score
5 mary 1998 100
2 rick 1994 90
1 tom 1995 88
The row names (5,2,1) show where in the
original dataset the output rows
come from.
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Phil Spector
> Sent: Tuesday, March 09, 2010 11:55 AM
> To: Tan, Richard
> Cc: r-help at r-project.org
> Subject: Re: [R] data frame select max group by like function
>
> Yet another way to do this with base R:
>
> > dat = read.csv(textConnection('ID, year, score
> + tom, 1995, 88
> + rick, 1994, 90
> + mary, 2000, 97
> + tom, 1998, 60
> + mary, 1998,100'))
> >
> do.call(rbind,lapply(split(dat,dat$ID),function(x)x[which.max(
x$score),]))
> ID year score
> mary mary 1998 100
> rick rick 1994 90
> tom tom 1995 88
>
> - Phil Spector
> Statistical Computing Facility
> Department of Statistics
> UC Berkeley
> spector at stat.berkeley.edu
>
>
> On Tue, 9 Mar 2010, Tan, Richard 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.
> >
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list