Jim Price
2008-Jul-03 18:19 UTC
[R] Re membering the last time an event occurred within a dataframe
All, I am constructing a pharmacokinetic dataset and have hit a snag. The dataset can be demonstrated in the following way: myData <- data.frame( evid = c(1, 0, 0, 0, 1, 0, 1, 1, 1, 0), time = 1:10, last.dose.time = c(1, 1, 1, 1, 5, 5, 7, 8, 9, 9) ) The evid field is an indicator variable for whether the associated observation is a dosing record (when it takes value 1) or an observation (where it takes value 0). The time field is a date-time record for the associated dose / observation event. I'm trying to calculate the time since the last dose for each observation event - to support that, the data I'd like to end up with is contained in last.dose.time, which gives the time at which the last dosing event occured. The problem is in calculating the last.dose.time field; this is the first time I've done this particular kind of data manipulation in R and I just can't get my head around the code to solve it. I've been eyeballing rle and I think there may be a solution hiding in there somewhere, but I'm still failing to progress. Any help would be appreciated! Thanks in advance, Jim Price Cardiome Pharma. Corp. -- View this message in context: http://www.nabble.com/Remembering-the-last-time-an-event-occurred-within-a-dataframe-tp18265464p18265464.html Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2008-Jul-03 18:38 UTC
[R] Re membering the last time an event occurred within a dataframe
Try: cumsum(myData$evid) On Thu, Jul 3, 2008 at 2:19 PM, Jim Price <price_ja at hotmail.com> wrote:> > All, > > I am constructing a pharmacokinetic dataset and have hit a snag. The dataset > can be demonstrated in the following way: > > > > myData <- data.frame( > evid = c(1, 0, 0, 0, 1, 0, 1, 1, 1, 0), > time = 1:10, > last.dose.time = c(1, 1, 1, 1, 5, 5, 7, 8, 9, 9) > ) > > > The evid field is an indicator variable for whether the associated > observation is a dosing record (when it takes value 1) or an observation > (where it takes value 0). The time field is a date-time record for the > associated dose / observation event. I'm trying to calculate the time since > the last dose for each observation event - to support that, the data I'd > like to end up with is contained in last.dose.time, which gives the time at > which the last dosing event occured. > > The problem is in calculating the last.dose.time field; this is the first > time I've done this particular kind of data manipulation in R and I just > can't get my head around the code to solve it. > > I've been eyeballing rle and I think there may be a solution hiding in there > somewhere, but I'm still failing to progress. Any help would be appreciated! > > > Thanks in advance, > > Jim Price > Cardiome Pharma. Corp. > > -- > View this message in context: http://www.nabble.com/Remembering-the-last-time-an-event-occurred-within-a-dataframe-tp18265464p18265464.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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. >
Henrique Dallazuanna
2008-Jul-03 18:41 UTC
[R] Re membering the last time an event occurred within a dataframe
If I understand, try this: with(myData, factor(cumsum(evid), labels = which(evid == 1))) On Thu, Jul 3, 2008 at 3:19 PM, Jim Price <price_ja@hotmail.com> wrote:> > All, > > I am constructing a pharmacokinetic dataset and have hit a snag. The > dataset > can be demonstrated in the following way: > > > > myData <- data.frame( > evid = c(1, 0, 0, 0, 1, 0, 1, 1, 1, 0), > time = 1:10, > last.dose.time = c(1, 1, 1, 1, 5, 5, 7, 8, 9, 9) > ) > > > The evid field is an indicator variable for whether the associated > observation is a dosing record (when it takes value 1) or an observation > (where it takes value 0). The time field is a date-time record for the > associated dose / observation event. I'm trying to calculate the time since > the last dose for each observation event - to support that, the data I'd > like to end up with is contained in last.dose.time, which gives the time at > which the last dosing event occured. > > The problem is in calculating the last.dose.time field; this is the first > time I've done this particular kind of data manipulation in R and I just > can't get my head around the code to solve it. > > I've been eyeballing rle and I think there may be a solution hiding in > there > somewhere, but I'm still failing to progress. Any help would be > appreciated! > > > Thanks in advance, > > Jim Price > Cardiome Pharma. Corp. > > -- > View this message in context: > http://www.nabble.com/Remembering-the-last-time-an-event-occurred-within-a-dataframe-tp18265464p18265464.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Duncan Murdoch
2008-Jul-03 18:45 UTC
[R] Re membering the last time an event occurred within a dataframe
On 7/3/2008 2:19 PM, Jim Price wrote:> All, > > I am constructing a pharmacokinetic dataset and have hit a snag. The dataset > can be demonstrated in the following way: > > > > myData <- data.frame( > evid = c(1, 0, 0, 0, 1, 0, 1, 1, 1, 0), > time = 1:10, > last.dose.time = c(1, 1, 1, 1, 5, 5, 7, 8, 9, 9) > ) > > > The evid field is an indicator variable for whether the associated > observation is a dosing record (when it takes value 1) or an observation > (where it takes value 0). The time field is a date-time record for the > associated dose / observation event. I'm trying to calculate the time since > the last dose for each observation event - to support that, the data I'd > like to end up with is contained in last.dose.time, which gives the time at > which the last dosing event occured. > > The problem is in calculating the last.dose.time field; this is the first > time I've done this particular kind of data manipulation in R and I just > can't get my head around the code to solve it. > > I've been eyeballing rle and I think there may be a solution hiding in there > somewhere, but I'm still failing to progress. Any help would be appreciated!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