[R] longitudinal study

arun smartpink111 at yahoo.com
Fri Jan 18 19:20:48 CET 2013


HI,

May be this helps:
dat1<-read.table(text="
id status week
1 no 1
1 no 2
1 no 3
1 no 4
1 no 5
1 no 6
1 no 7
2 no 1
2 no 2
2 no 3
2 no 4
2 yes 5
2 yes 6
2 na 7
2 na 8
2 na 9
3 no 1
3 no 2
3 no 3
3 Unknown 4
3 unknown 5
3 na 6
3 na 7
3 na 8 
",sep="",header=TRUE,stringsAsFactors=FALSE,na.strings="na")
 dat2<-dat1[complete.cases(dat1),]
 res<-do.call(rbind,lapply(split(dat2,dat2$id),function(x) rbind(tail(x[all(x[,2]=="no")],1),head(x[x[,2]=="yes"|x[,2]=="Unknown",],1))))
 res
#  id  status week
#1  1      no    7
#2  2     yes    5
#3  3 Unknown    4
A.K.





----- Original Message -----
From: Rich Shepard <rshepard at appl-ecosys.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Friday, January 18, 2013 12:18 PM
Subject: Re: [R] longitudinal study

On Fri, 18 Jan 2013, bibek sharma wrote:

> I have a data set from a longitudinal study ( sample below) where subjects
> are followed over time. Second column (status) contains info about if
> subject is dead or still in the study and third column is time measured in
> the week. Here is what I need: if status is not dead or unknown take the
> last week, if status is dead or unknown I need to have corresponding week.
>
> Desired resulst:
>
> 1    no    7
> 2    yes    5
> 3    Unknown    4

   Looks like a survival analysis situation. I know there are R packages for
this.

Rich

______________________________________________
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.




More information about the R-help mailing list