Skip to content

Selecting First Incidence from Longitudinal Data

2 messages · arun, Frank E Harrell Jr

#
Hi,

I am not sure why you are getting different results.? I couldn't reproduce your problem.
dat1<- read.table(text=" 
ID??? COMPL? SEX? HEREDITY 
1??? 0????? 1????? 2 
1??? 0????? 1????? 2 
1??? 3????? 1????? 2 
2??? 0????? 0????? 1 
2??? 1????? 0????? 1 
2??? 2????? 0????? 1 
2??? 2????? 0????? 1 
3??? 0????? 0????? 1 
3??? 0????? 0????? 1 
3??? 0????? 0????? 1 
3??? 0????? 0????? 1 
3??? 2????? 0????? 1 
4??? 0????? 1????? 2 
4??? 0????? 1????? 2 
",sep="",header=TRUE)
do.call(rbind,lapply(split(dat1,dat1$ID),function(x) if(any(x$COMPL!=0)) head(x[x$COMPL!=0,],1) else head(x,1)))
#? ID COMPL SEX HEREDITY
#1? 1???? 3?? 1??????? 2
#2? 2???? 1?? 0??????? 1
#3? 3???? 2?? 0??????? 1
#4? 4???? 0?? 1??????? 2


You could also try:
dat1[with(dat1,ave(COMPL,ID,FUN=function(x) if(any(x!=0)) cumsum(x>0) else seq_along(x)))==1,] #modification of David's code
#?? ID COMPL SEX HEREDITY
#3?? 1???? 3?? 1??????? 2
#5?? 2???? 1?? 0??????? 1
#12? 3???? 2?? 0??????? 1
#13? 4???? 0?? 1??????? 2
A.K.
#
I think we need a task view on longitudinal data manipulation.  There are so
many approaches to this - people need help navigating them.

I tend to stay away from the lapply-split methods as they don't look quite
as clean and may take longer to run.  The aggregate function uses too much
data frame subscripting.  The plyr package and the mApply function in the
Hmisc package provide some other nice solutions.  Often I like to stick with
tapply using constructs like 

with(mydata, tapply(1:nrow(mydata), subjectID, function(i) {... operate on
variables in mydata subscripted by [i] ...)))

Frank


arun kirshna wrote

            
head(x[x$COMPL!=0,],1) else head(x,1)))

            
patients for which no complication occurred. That is, for my data I want to
include patient no. 4, for which the COMPL value will be 0.
by=list(dat0$ID),FUN=length)$x, FUN=function(x){seq(1, x)})))
by=list(dat1$ID),FUN=length)$x, FUN=function(x){seq(1, x)})))
COMPL!=0

            

            

            
http://www.R-project.org/posting-guide.html

            
-----
Frank Harrell
Department of Biostatistics, Vanderbilt University
--
View this message in context: http://r.789695.n4.nabble.com/Selecting-First-Incidence-from-Longitudinal-Data-tp4659455p4659530.html
Sent from the R help mailing list archive at Nabble.com.