[R] Aggregate, max and time of max
Carlos J. Gil Bellosta
cgb at datanalytics.com
Sat Jul 25 02:23:28 CEST 2009
Hello,
I believe that
by( data.ex, data.ex[,c(3,4)], function(x) x[which.max(x[,1]),] )
does what you want. Then,
do.call( rbind, by( data.ex, data.ex[,c(3,4)], function(x)
x[which.max(x[,1]),] ) )
looks somewhat nicer.
Best regards,
Carlos J. Gil Bellosta
http://www.datanalytics.com
On Fri, 2009-07-24 at 13:06 -0400, Afshartous, David wrote:
> 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
>
> ______________________________________________
> 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.
More information about the R-help
mailing list