Hi:

Here are three ways, but there are others:

# (1)
# package doBy:

library(doBy)
summaryBy(value + value2 ~ class, data = df)
  class value.mean value2.mean
1     x        2.5         2.0
2     y        8.0         2.0
3     z       13.5         5.5

# (2)  aggregate():

with(df, aggregate(list(val1 = value, val2 = value2), list(class), FUN =
mean))
  Group.1 val1 val2
1       x  2.5  2.0
2       y  8.0  2.0
3       z 13.5  5.5

# plyr solution
library(plyr)
ddply(df, .(class), summarise, val1 = mean(value), val2 = mean(value2))
  class val1 val2
1     x  2.5  2.0
2     y  8.0  2.0
3     z 13.5  5.5

HTH,
Dennis

On Mon, Feb 22, 2010 at 1:13 AM, mirauta <bmirauta@yahoo.com> wrote:

>
> Hello,
>
> Having the matrix d
> > d
>   value value2 class
> 1      1      1     x
> 2      2      2     x
> 3      3      3     x
> 4      4      2     x
> 5      5      1     y
> 6     11      3     y
> 7     12      4     z
> 8     13      5     z
> 9     14      6     z
> 10    15      7     z
>
> I want to calculate the means and cov matrix for groups x,y,z.
> I know how to do it the long way.
> I tried to use tapply and kmeans but no good results.
> (tapply is a good solution for only 1 variable)
>
>
> Thank you
>
> Bogdan
>
>
> --
> View this message in context:
> http://n4.nabble.com/how-do-I-calculate-means-or-cov-matrix-for-multivariate-groups-tp1564157p1564157.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]

