Skip to content

seeking tip to keep first of multiple observations per ID

4 messages · Julie Royster, John Kane, arun +1 more

#
Probably the first thing to do is supply some sample data 
See https://github.com/hadley/devtools/wiki/Reproducibility for some suggestions.

However you may want to take a look at http://stackoverflow.com/questions/13279582/select-only-the-first-rows-for-each-unique-value-of-a-column-in-r particularly at answer number 3 which uses the data.table package and  which looks like it may do what you want.  

John Kane
Kingston ON Canada
____________________________________________________________
FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!
#
Hi,

If you can dput() a small part of your dataset e.g. dput(head(yourdataset),20)), it would be helpful.

Otherwise, 

dat1<- data.frame(ID=rep(1:3,times=c(3,4,2)),col2=rnorm(9))
?aggregate(.~ID,data=dat1,head,1)
#? ID?????? col2
#1? 1 -0.0637622
#2? 2? 1.1782429
#3? 3? 0.4670021
A.K.



----- Original Message -----
From: Julie Royster <jsdroyster at bellsouth.net>
To: r-help at r-project.org
Cc: 
Sent: Friday, March 15, 2013 12:06 PM
Subject: [R] seeking tip to keep first of multiple observations per ID

Dear R community,

I am a neophyte and I cannot figure out how to accomplish keeping only the
first record for each ID in a data.frame that has assorted numbers of
records per ID.

I studied and found references to packages plyr and sql for R, and I fear
the documentation for those was over my head and I could not identify what
may be there to reach my goal.

If someone could point me toward a method I will gladly study documentation,
or if there is an example posted someplace I will follow it.

THANKS!
Julie 

??? [[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.