[R] data frame select max group by like function
Bert Gunter
gunter.berton at gene.com
Tue Mar 9 20:37:34 CET 2010
An alternative base R version:
?by
by(Dat,Dat$ID,function(frm)frm$year[which.max(frm$score)])
by() is a wrapper for tapply(), one of the base R "apply" family of
functions (lapply, mapply, etc.) for which many find the plyr package
provides a simpler and more consistent interface. I'm used to the base R
versions, however, and in fact my preferred approach to this is simply:
with(Dat,
tapply(seq_len(nrow(Dat)),ID,function(i)year[i][which.max(score[i])]))
In any case, you should invest the time to learn either (or both) the base R
or plyr version of the apply() functions.
Bert Gunter
Genentech Nonclinical Biostatistics
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ista Zahn
Sent: Tuesday, March 09, 2010 11:00 AM
To: Tan, Richard
Cc: r-help at r-project.org
Subject: Re: [R] data frame select max group by like function
Hi Richard,
There are probably better ways, but here is one approach:
sc
MaxScore <- aggregate(Dat$score, list(Dat$ID), max)
names(MaxScore) <- c("ID", "score")
MaxYear <- merge(Dat, MaxScore)
Best,
Ista
On Tue, Mar 9, 2010 at 1: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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
______________________________________________
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