On Sep 9, 2010, at 5:47 PM, array chip wrote:

Hi, suppose I have a data frame as below:

dat<- cbind (expand .grid (id=c(1,2,3),time=c(0,3,6),mode=c('R','L'),rep=(1:3)),y=rnorm(54))


I kind of want to "squeeze" the data frame into a new one with averaged "y" over "rep" for the same id, time and mode. taking average is easy with tapply:

tapply(dat$y, list(dat$id, dat$time, dat$mode), mean)

Try:

dat$avg.y <- ave(dat$y, dat$id, dat$time, dat$mode, FUN=mean)

?ave

The syntax of ave is different than "tapply" or "by". The grouping factors are not presented as a list and the FUN argument comes after the ,..., so it needs to be named if different than the default of "mean".


But I want the result to be in the same format as "dat". Certainly, we always can transform the result (array) into the data frame using a lot of codes. But
is there a simple way to do this?

Thanks

David Winsemius, MD
West Hartford, CT

______________________________________________
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.

Reply via email to