Julie Royster
2013-Mar-15 16:06 UTC
[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]]
John Kane
2013-Mar-15 19:03 UTC
[R] seeking tip to keep first of multiple observations per ID
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> -----Original Message----- > From: jsdroyster at bellsouth.net > Sent: Fri, 15 Mar 2013 12:06:05 -0400 > To: r-help at r-project.org > 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.____________________________________________________________ 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.
pedroabg
2013-Mar-15 19:34 UTC
[R] seeking tip to keep first of multiple observations per ID
I had the same problem. -- View this message in context: http://r.789695.n4.nabble.com/seeking-tip-to-keep-first-of-multiple-observations-per-ID-tp4661520p4661534.html Sent from the R help mailing list archive at Nabble.com.