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(structure(
t(matrix(unlist(b),nrow=length(b[[1]]))),
dimnames=list(names(b),col.names=names(b[[1]]))
))
}
The idea is the same as a SAS MEANS BY statement. The function 'means.by' takes a data.frame and an indexing variable and computes the mean over the columns of the data.frame for each set of rows corresponding to the unique values of INDEX and returns a new data frame with with the row names the unique values of INDEX.
I'm sure there must be a better way to do this in R but I couldn't think of anything.