Skip to content

Selecting First Incidence from Longitudinal Data

11 messages · David Winsemius, Rui Barradas, Xiaogang Su +2 more

#
Hi,
Try this:
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)
library(plyr)
dat2<- dat1[ddply(dat1,.(ID),summarise,COMPL!=0)[,2],]
?aggregate(.~ID,data=dat2,head,1)
#? ID COMPL SEX HEREDITY
#1? 1???? 3?? 1??????? 2
#2? 2???? 1?? 0??????? 1
#3? 3???? 2?? 0??????? 1
A.K. 




----- Original Message -----
From: Tasnuva Tabassum <t.tasnuva at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, February 23, 2013 9:28 AM
Subject: [R] Selecting First Incidence from Longitudinal Data

I have a longitudinal competing risk data of the form:

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.

Where, COMPL= health complication of diabetic patients which has value
labels?  as? 0= no complication,1=coronary heart disease, 2=retinopathy, 3=
nephropathy.


I want to select only the first complication that occurred to each patient.
What R function can I use?

??? [[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.
#
On Feb 23, 2013, at 6:28 AM, Tasnuva Tabassum wrote:

            
ID COMPL SEX HEREDITY
3   1     3   1        2
5   2     1   0        1
12  3     2   0        1
David Winsemius
Alameda, CA, USA
#
Hi,
You can also use:
?do.call(rbind,lapply(split(dat1,dat1$ID),function(x) head(x[x$COMPL!=0,],1)))
#? ID COMPL SEX HEREDITY
#1? 1???? 3?? 1??????? 2
#2? 2???? 1?? 0??????? 1
#3? 3???? 2?? 0??????? 1






----- Original Message -----
From: Tasnuva Tabassum <t.tasnuva at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Saturday, February 23, 2013 9:28 AM
Subject: [R] Selecting First Incidence from Longitudinal Data

I have a longitudinal competing risk data of the form:

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.

Where, COMPL= health complication of diabetic patients which has value
labels?  as? 0= no complication,1=coronary heart disease, 2=retinopathy, 3=
nephropathy.


I want to select only the first complication that occurred to each patient.
What R function can I use?

??? [[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.
#
Hello,

You can use ?aggregate and ?head to do what you want. Try the following.



dat <- 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
", header = TRUE)

aggregate(. ~ ID, data = subset(dat, COMPL != 0), head, 1)


Hope this helps,

Rui Barradas

Em 23-02-2013 14:28, Tasnuva Tabassum escreveu:
#
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:

            

  
    
#
Hi,
Try this:
#dat1
?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
A.K.