Is there a way that this can be improved, or done more simply?
means.by<-function(data,INDEX){ b<-by(data,INDEX,function(d)apply(d,2,mean)) return(
Does the aggregate function do what you want?
If not, look at the plyr package, it gives several options for taking things apart, doing computations on the pieces, then putting it back together again.
You may also be able to do this using the reshape package.