You could use a combination of sorting (to put the date in order) and
then use duplicated() (on the ID), something like
df <- data.frame(ID=rep(1:10,rep(2,10)),Date=rep(1:2,10),X=rnorm(20))
df <- sort.col(df,"@ALL",c("ID","Date"),c(T,F)) # asc=F puts Date in
reverse
# order
df[!duplicated(df$ID),]
with aggregate, this could be done as
aggregate(df[,c("Date","X")],by=list(ID=df$ID),function(x) x[x==max(x)])
without having to do the sorting.
These both have the problem, however, that two observations with the
same ID on the same date are not retained, so be certain that this won't
be a rub. To get around that, using split and lapply should help, but
piecing the list of data frames back to a single data frame is a
nuisance.
Brad
Brad Biggerstaff, Ph.D.
(970) 221-6473 ... BBiggerstaff@cdc.gov
---------------------------------------------------------------------
> -----Original Message-----
> From: s-news-owner@lists.biostat.wustl.edu [mailto:s-news-
> owner@lists.biostat.wustl.edu] On Behalf Of Schwarz,Paul
> Sent: Thursday, November 17, 2005 12:11 AM
> To: s-news@lists.biostat.wustl.edu
> Subject: [S] how to extract entire rows from a data frame based on a
index
> variable?
>
> Dear S-News readers,
>
> I'm puzzling over how to solve the following problem. Let's say that I
> have a data frame with possibly multiple observations (rows) per
> sampling unit, in this case survey respondents. In addition to a
> respondent ID variable, there is a date variable tracking the date the
> information was collected. If I want to extract from this data frame
> only the most recent observation (or the first, etc.) for each
> respondent, how could I do this? I've been thinking about this problem
> as a tapply()-like problem where the respondent ID would be the index
> variable, but I haven't figured how to write a function to extract and
> return entire rows. I would appreciate ideas or suggestions from
fellow
> list readers on how to solve this.
>
> Thanks for your help.
>
> -Paul
> --------------------------------------------------------------------
> This message was distributed by s-news@lists.biostat.wustl.edu. To
> unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
> the BODY of the message: unsubscribe s-news
|