To account for COMP, dat$sequence <- as.vector(unlist(lapply( aggregate(dat$ID, by=list(dat$ID), FUN=length)$x, FUN=function(x){seq(1, x)))) dat0 <- dat[dat$sequence==1 & dat$COMPL!= 0, ]
HTH, X On Sat, Feb 23, 2013 at 1:15 PM, Xiaogang Su <xiaogan...@gmail.com> wrote: > 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 <ruipbarra...@sapo.pt>wrote: > >> 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: >> >> 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@r-project.org mailing list >>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >>> PLEASE do read the posting guide http://www.R-project.org/** >>> posting-guide.html <http://www.R-project.org/posting-guide.html> >>> and provide commented, minimal, self-contained, reproducible code. >>> >>> >> ______________________________**________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > ============================== > Xiaogang Su, Ph.D. > Associate Professor & Statistician > School of Nursing, University of Alabama > Birmingham, AL 35294-1210 > (205) 934-2355 [Office] > x...@uab.edu > xiaogan...@gmail.com > https://sites.google.com/site/xgsu00/ -- ============================== Xiaogang Su, Ph.D. Associate Professor & Statistician School of Nursing, University of Alabama Birmingham, AL 35294-1210 (205) 934-2355 [Office] x...@uab.edu xiaogan...@gmail.com https://sites.google.com/site/xgsu00/ [[alternative HTML version deleted]] ______________________________________________ R-help@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.