Skip to content
Prev 318185 / 398506 Next

Selecting First Incidence from Longitudinal Data

HI,
Tried your approach:


?dat1$sequence <- as.vector(unlist(lapply( aggregate(dat1$ID, by=list(dat1$ID),FUN=length)$x, FUN=function(x){seq(1, x)}))) 
?dat0 <- dat1[dat1$sequence==1 & dat1$COMPL!= 0, ] #your second solution
?dat0
#[1] ID?????? COMPL??? SEX????? HEREDITY sequence
#<0 rows> (or 0-length row.names)
? 

dat1[dat1$sequence==1,] #here the OP wanted first incidence where COMPL!=0
#?? ID COMPL SEX HEREDITY sequence
#1?? 1???? 0?? 1??????? 2??????? 1
#4?? 2???? 0?? 0??????? 1??????? 1
#8?? 3???? 0?? 0??????? 1??????? 1
#13? 4???? 0?? 1??????? 2??????? 1
A.K.



----- Original Message -----
From: Xiaogang Su <xiaogangsu at gmail.com>
To: Rui Barradas <ruipbarradas at sapo.pt>
Cc: r-help at r-project.org
Sent: Saturday, February 23, 2013 2:15 PM
Subject: Re: [R] Selecting First Incidence from Longitudinal Data

Try this:
dat$sequence <- as.vector(unlist(lapply( aggregate(dat$ID, by=list(x),
FUN=length)$x, FUN=function(x){seq(1, x))))
dat0 <- dat[dat$sequence==1, ]

HTH, X
On Sat, Feb 23, 2013 at 1:07 PM, Rui Barradas <ruipbarradas at sapo.pt> wrote: