Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compute means of a group by factor

Tags:

r

statistics

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.

like image 331
Andrew Redd Avatar asked Oct 04 '10 19:10

Andrew Redd


1 Answers

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.

like image 81
Greg Snow Avatar answered Oct 29 '22 13:10

Greg Snow