[R] Aggregate, max and time of max

Afshartous, David DAfshartous at med.miami.edu
Fri Jul 24 19:06:56 CEST 2009


All,

For data consisting of serial measurements on subjects, one may use the
aggregate function to say compute the peak response for each subject for
each design condition.  Is there a way to alter this or another one-liner to
also retain the time at which the peak occurred and thus avoid writing a
doing this via a loop?  I suppose one could attempt to employ the split
function but that's probably no simpler than employing a loop.  Sample code
below:


data = expand.grid(time = seq(1,6), subject = seq(1,20), treatment =
c("placebo", "drug"))
data.ex = cbind(y = rnorm(dim(data)[1], 5, 1), data.ex)

data.peak = aggregate(data.ex[c(1)], data.ex[c(3,4)], max)
## this provides the peak of each subject on each treatment, but time is
## lost.  Including time in the statement doesn't help clearly as then
## the peak of all the times will be calculated


David

--------------------------------------
David Afshartous, Ph.D.
Research Assistant Professor
University of Miami, Miller School of Medicine
Division of Clinical Pharmacology
1500 N.W. 12th Avenue, 15th Floor West
Miami, Florida 33136

E-mail: afshar at med.miami.edu
Phone: +1 305-243-1549




More information about the R-help mailing list