Luwis Tapiwa Diya
2008-Aug-07 10:21 UTC
[R] Obtaining the first /or last record of a subject in a longitudinal study
Dear R users, I was wondering if anyone knows how to obtain(subset) the first and/or the last record of a subject in a longitudinal setup. Normally in SAS one uses first.variable1 and last.variable1. So my question is that is there an R way of doing this. Regards, -- Luwis Diya, Phd student (Biostatistics), Biostatistical Center, School Of Public Health, Catholic University of Leuven, U.Z. St Raphael, Kapucijnenvoer 35, B-3000 Leuven, Belgium, Cell: +32(0)497 22 94 83 Phone:+32(0)16 32 68 86 [Office] Phone:+32(0)16 32 98 76 [Home] Fax: +32(0)16 33 70 15 Email: luwis.diya@med.kuleuven.be http://med.kuleuven.be/biostat/staff/LD.htm [[alternative HTML version deleted]]
Chuck Cleland
2008-Aug-07 10:59 UTC
[R] Obtaining the first /or last record of a subject in a longitudinal study
On 8/7/2008 6:21 AM, Luwis Tapiwa Diya wrote:> Dear R users, > > I was wondering if anyone knows how to obtain(subset) the first and/or the > last record of a subject in a longitudinal setup. > > Normally in SAS one uses first.variable1 and last.variable1. So my question > is that is there an R way of doing this. > > Regards,How about a combination of aggregate() and head() or tail()? > library(nlme) > aggregate(Oxboys[,-1], list(Subject = Oxboys$Subject), head, n=1) Subject age height Occasion 1 10 -1 126.2 1 2 26 -1 132.2 1 3 25 -1 135.5 1 4 9 -1 132.7 1 5 2 -1 136.9 1 6 6 -1 142.4 1 7 7 -1 141.3 1 8 17 -1 134.9 1 9 16 -1 142.8 1 10 15 -1 137.5 1 11 8 -1 141.7 1 12 20 -1 146.5 1 13 1 -1 140.5 1 14 18 -1 145.5 1 15 5 -1 145.8 1 16 23 -1 144.5 1 17 11 -1 142.5 1 18 21 -1 143.9 1 19 3 -1 150.0 1 20 24 -1 147.8 1 21 22 -1 147.4 1 22 12 -1 149.9 1 23 13 -1 148.9 1 24 14 -1 151.6 1 25 19 -1 156.9 1 26 4 -1 155.7 1 > aggregate(Oxboys[,-1], list(Subject = Oxboys$Subject), tail, n=1) .. -- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Daniel Malter
2008-Aug-07 13:36 UTC
[R] Obtaining the first /or last record of a subject in alongitudinal study
Had the same problem recently. I used a loop. Assume your data frame is called "data" and your units of observation are "Subject". first.round.index=NULL final.round.index=NULL for(i in unique(Subject)){ first.round.index[i]=min(which(Subject==i)) final.round.index[i]=max(which(Subject==i)) } first.round.index=first.round.index[is.na(first.round.index)==F] final.round.index=final.round.index[is.na(final.round.index)==F] ##This gives you the row numbers for first, respectively last, observation for each subject ##Then you can select the data rows of your data according to first- or final.row.index, e.g.: data.first.round=data[first.round.index, ] data.final.round=data[final.round.index, ] ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von Luwis Tapiwa Diya Gesendet: Thursday, August 07, 2008 6:22 AM An: R-help at r-project.org Betreff: [R] Obtaining the first /or last record of a subject in alongitudinal study Dear R users, I was wondering if anyone knows how to obtain(subset) the first and/or the last record of a subject in a longitudinal setup. Normally in SAS one uses first.variable1 and last.variable1. So my question is that is there an R way of doing this. Regards, -- Luwis Diya, Phd student (Biostatistics), Biostatistical Center, School Of Public Health, Catholic University of Leuven, U.Z. St Raphael, Kapucijnenvoer 35, B-3000 Leuven, Belgium, Cell: +32(0)497 22 94 83 Phone:+32(0)16 32 68 86 [Office] Phone:+32(0)16 32 98 76 [Home] Fax: +32(0)16 33 70 15 Email: luwis.diya at med.kuleuven.be http://med.kuleuven.be/biostat/staff/LD.htm [[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.
Frank E Harrell Jr
2008-Aug-07 14:38 UTC
[R] Obtaining the first /or last record of a subject in alongitudinal study
A very fast way using tapply may be found in section 4.3.8 of http://biostat.mc.vanderbilt.edu/twiki/pub/Main/RS/sintro.pdf Frank Daniel Malter wrote:> Had the same problem recently. I used a loop. Assume your data frame is > called "data" and your units of observation are "Subject". > > first.round.index=NULL > final.round.index=NULL > for(i in unique(Subject)){ > first.round.index[i]=min(which(Subject==i)) > final.round.index[i]=max(which(Subject==i)) > } > > first.round.index=first.round.index[is.na(first.round.index)==F] > final.round.index=final.round.index[is.na(final.round.index)==F] > > ##This gives you the row numbers for first, respectively last, observation > for each subject > > ##Then you can select the data rows of your data according to first- or > final.row.index, e.g.: > > data.first.round=data[first.round.index, ] > data.final.round=data[final.round.index, ] > > > ------------------------- > cuncta stricte discussurus > ------------------------- > > -----Urspr?ngliche Nachricht----- > Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im > Auftrag von Luwis Tapiwa Diya > Gesendet: Thursday, August 07, 2008 6:22 AM > An: R-help at r-project.org > Betreff: [R] Obtaining the first /or last record of a subject in > alongitudinal study > > Dear R users, > > I was wondering if anyone knows how to obtain(subset) the first and/or the > last record of a subject in a longitudinal setup. > > Normally in SAS one uses first.variable1 and last.variable1. So my question > is that is there an R way of doing this. > > Regards, > > -- > Luwis Diya, Phd student (Biostatistics), Biostatistical Center, School Of > Public Health, Catholic University of Leuven, U.Z. St Raphael, > Kapucijnenvoer 35, B-3000 Leuven, Belgium, > > Cell: +32(0)497 22 94 83 > Phone:+32(0)16 32 68 86 [Office] > Phone:+32(0)16 32 98 76 [Home] > Fax: +32(0)16 33 70 15 > > Email: luwis.diya at med.kuleuven.be > http://med.kuleuven.be/biostat/staff/LD.htm-- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
Gabor Grothendieck
2008-Aug-07 15:03 UTC
[R] Obtaining the first /or last record of a subject in a longitudinal study
It would have helped to see an example to clarify the input and output to the problem but !duplicated(x) will be TRUE for the first of each set of equal values in x and !duplicated(x, fromLast = TRUE) for the last. See ?duplicated for details. On Thu, Aug 7, 2008 at 6:21 AM, Luwis Tapiwa Diya <siwulayid at gmail.com> wrote:> Dear R users, > > I was wondering if anyone knows how to obtain(subset) the first and/or the > last record of a subject in a longitudinal setup. > > Normally in SAS one uses first.variable1 and last.variable1. So my question > is that is there an R way of doing this. > > Regards, > > -- > Luwis Diya, Phd student (Biostatistics), > Biostatistical Center, > School Of Public Health, > Catholic University of Leuven, > U.Z. St Raphael, > Kapucijnenvoer 35, > B-3000 Leuven, > Belgium, > > Cell: +32(0)497 22 94 83 > Phone:+32(0)16 32 68 86 [Office] > Phone:+32(0)16 32 98 76 [Home] > Fax: +32(0)16 33 70 15 > > Email: luwis.diya at med.kuleuven.be > http://med.kuleuven.be/biostat/staff/LD.htm > > [[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. >
Possibly Parallel Threads
- R2WinBUGS and R-2.10.0: The school example not running
- Printing the function t.test() in R
- Cross classified or Multiple membership or Hierarchical (3 level ) logistic models using Umacs
- Help in Compliling user -defined functions in Rpart
- codamenu() :Error in coda.options....