[R] Method Guidance
Leonard Mada
|eo@m@d@ @end|ng |rom @yon|c@eu
Fri Jan 14 23:22:55 CET 2022
Dear Jeff,
I am sending an updated version of the code.
The initial version assumed that the time points correspond to an
integer sequence. The code would fail for arbitrary times.
The new code is robust. I still assume that the data is in column-format
and that you want the time to the previous "A"-event, even if there are
other non-A events in between.
The code is similar, but we cannot use seq(0, x-1) anymore. Instead, we
will repeat the time point of the previous A-event. (Last-A Carried forward)
# jrdf = the data frame from my previous mail;
cumEvent = cumsum(jrdf$Event_A);
# we cannot use the actual values of the cumsum,
# but will use the number of (same) values to the previous event;
freqEvent = rle(cumEvent);
freqEvent = freqEvent$lengths;
# repeat the time-points
timesA = jrdf$Time[jrdf$Event_A == 1];
sameTime = rep(timesA, freqEvent);
timeToA = jrdf$Time - sameTime;
### Step 2:
# extract/view the times (as before);
timeToA[jrdf$Event_B >= 1];
# Every Time to A: e.g. for multiple extractions;
cbind(jrdf, timeToA);
# Time to A only for B: set non-B to 0;
# Note:
- the rle() function might be less known;
- it is "equivalent" to:
tbl = table(cumEvent);
# to be on the safe side (as the cumsum is increasing):
id = order(as.numeric(names(tbl)));
tbl = tbl[id];
Hope this helps,
Leonard
On 1/14/2022 3:30 AM, Leonard Mada wrote:
> 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