Skip to content
Prev 148954 / 398498 Next

Re membering the last time an event occurred within a dataframe

On 7/3/2008 2:19 PM, Jim Price wrote:
This may not be the easiest way, but it's very general:  create a 
function that returns the last time, and evaluate it at all of the 
times.  For example,

Extract just the dosing times:

 > sub <- subset(myData, evid == 1)

Create the step function:

 > f <- approxfun(sub$time, sub$time, method="constant", rule=2)

Evaluate it:

 > f(myData$time)
  [1] 1 1 1 1 5 5 7 8 9 9

The construction of f assumes that the times are in increasing order, 
and its definition assumes you have no observations before the earliest 
dosing time.  You'll need a bit more  fiddling (sort the times, figure 
out what value to give before dosing) if those assumptions don't hold.

Duncan Murdoch