[R] Method Guidance

Leonard Mada |eo@m@d@ @end|ng |rom @yon|c@eu
Fri Jan 14 02:30:51 CET 2022


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



More information about the R-help mailing list