[R] How to subset a data frame to include only first events

arun smartpink111 at yahoo.com
Tue Feb 5 14:30:00 CET 2013


If the  `Date` column is not ordered:

 aggregate(Date1~ID,data=dat1,function(x) min(x))
#  ID      Date1
#1  1 2012-01-05
#2  2 2012-01-09
#3  3 2012-01-03

#If it is ordered:


dat2<- data.frame(ID=rep(1:3,each=3),Date2)
 # ID      Date2
#1  1 2012-01-05
#2  2 2012-01-09
#3  3 2012-01-03

----- Original Message -----
From: Dylan Arena <darena at stanford.edu>
To: r-help at r-project.org
Sent: Monday, February 4, 2013 9:29 PM
Subject: [R] How to subset a data frame to include only first events

Hi there,

I have data frame with columns ID and Date.  There are multiple rows for
each ID, but I only want to keep the *first* such row--i.e., the row
corresponding to the earliest event.  So if I had, say, 1000 rows of 100
IDs doing an average of ten events each, I'd run this trimming procedure
and end up with a data frame containing 100 rows (one for each ID), where
each row record that ID's first event.

I can think of slow, clumsy, for-loop ways to trim the data frame, but I'm
hopeful that there is some slick "R" way to do it that someone here can
help me find.  But so deep is my ignorance that I can't even come up with
useful search terms to use on Rseek.org (I investigated "merge" but had no
luck there).

Grateful for any ideas/tips/pointers,

    [[alternative HTML version deleted]]

R-help at r-project.org mailing list
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