Dear Jeff,
My answer is a little bit late, but I hope it helps. jrdf = read.table(text="Time Event_A Event_B Lag_B 1 1 1 0 2 0 1 1 3 0 0 0 4 1 0 0 5 0 1 1 6 0 0 0 7 0 1 3 8 1 1 0 9 0 0 0 10 0 1 2", header=TRUE, stringsAsFactors=FALSE) Assuming that: - Time, Event_A, Event_B are given; - Lag_B needs to be computed; Step 1: - compute time to previous Event A; tmp = jrdf[, c(1,2)]; # add an extra event so last rows are not lost: tmp = rbind(tmp, c(nrow(tmp) + 1, 1)); timeBetweenA = diff(tmp$Time[tmp$Event_A > 0]); timeToA = unlist(sapply(timeBetweenA, function(x) seq(0, x-1))) ### Step 2: # - extract the times; timeToA[jrdf$Event_B >= 1]; cbind(jrdf, timeToA); Sincerely, Leonard ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.