Try this: transform(DF, Time = ave(1:nrow(DF), Subject, FUN = function(ix) if (any(Marker[ix] == 1)) Time - Time[Marker == 1] else Time))
On Fri, Dec 4, 2009 at 5:47 PM, Dennis Fisher <fis...@plessthan.com> wrote: > Colleagues, > > R 2.9.0 on all platforms > > I have a dataset that contains three columns of interest: ID's, serial > elapsed times, and a marker. Representative data: > Subject Time Marker > 1 100.5 0 > 1 101 0 > 1 102 1 > 1 103 0 > 1 105 0 > > For each subject, I would like to find the time associated with MARKER == > 1, then replace Time with Time - (Time[Marker == 1]) > The result for this subject would be: > Subject Time Marker > 1 -1.5 0 > 1 -1 0 > 1 0 1 > 1 1 0 > 1 3 0 > > One proviso: some subjects do not have Marker == 1; for these subjects, I > would like Time to remain unchanged. > > At present, I am looping over each subject. The number of subjects is > large so this process is quite slow. I assume that one of the apply > functions could speed this markedly but I am not facile with them. Any help > would be appreciated. > > Dennis > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-866-PLessThan (1-866-753-7784) > www.PLessThan.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. > [[alternative HTML version deleted]] ______________________________________________ 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.