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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With