[R] Obtaining the first /or last record of a subject in alongitudinal study

Frank E Harrell Jr f.harrell at vanderbilt.edu
Thu Aug 7 16:38:03 CEST 2008


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



More information about the R-help mailing list