I have a data frame that contains ID variables, timepoints, and response values. I'm using some old code that uses ddply to calculate the maximum mean values for each ID variable (i.e., the timepoint with the highest response in each group):
grp <- rep(c("a", "b"), each=6)
t <- rep(1:2, each=3, len=12)
y <- rnorm(length(t))
df <- data.frame(grp=factor(grp), t=t, y=y)
meanY <- ddply(df, .(grp, t), function(x) mean(x$y))
maxMeanY <- ddply(meanY, .(grp), function(x) max(x$V1))
I have now decided that I would also like to know which time value goes with each maximum mean value:
maxTimes
> grp t V1
1 a 2 0.0534
2 b 1 0.6015
i.e., telling me that for group a, t2 had the highest average, but for group b, t1 had the highest average.
What is the easiest way to get this?