On Thu, Nov 6, 2008 at 4:23 PM, Peter Jepsen <[EMAIL PROTECTED]> wrote: > > Here is an example: > > id <- c(rep("a",4),rep("b",2), rep("c",5), rep("d",1)) > start <- c(c(0,6,17,20),c(0,1),c(0,5,10,11,50),c(0)) > stop <- c(c(6,12,20,30),c(1,10),c(3,10,11,30,55),c(6)) > data <- as.data.frame(cbind(id,start,stop)) > data > # id start stop > # 1 a 0 6 > # 2 a 6 12 > # 3 a 17 20 > # 4 a 20 30 > # 5 b 0 1 > # 6 b 1 10 > # 7 c 0 3 > # 8 c 5 10 > # 9 c 10 11 > # 10 c 11 30 > # 11 c 50 55 > # 12 d 0 6 > > So, what I want to end up with is this: > > id start stop > a 0 12 # This patient was transferred at time 6 and discharged at > time 12. The admission starting at time 17 is therefore irrelevant. > b 0 10 > c 0 3 > d 0 6 >
Try this: result <- list() num <- length(levels(factor(data$id))) length(result) <- 3*num dim(result) <- c(3,num) result <- data[data$start == 0,] Y <- as.integer(row.names(result)) for (i in 1:num) { if (Y[i] == dim(data)[1]) (result[i,3] <- data[dim(data)[1],3]) else (result[i,3] <- data[Y[i]+1,3]) } result Sorry it is ugly cuz i am new too but hopefully it gives you some ideas. ______________________________________________ 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.