mirauta
2010-Feb-22 09:13 UTC
[R] how do I calculate means or cov matrix for multivariate groups
Hello, Having the matrix d> dvalue 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.
Dennis Murphy
2010-Feb-22 09:36 UTC
[R] how do I calculate means or cov matrix for multivariate groups
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]]
Henrique Dallazuanna
2010-Feb-22 12:19 UTC
[R] how do I calculate means or cov matrix for multivariate groups
Try this: lapply(list(mean, cov), by, data = d[,1:2], INDICES = d[,'class']) On Mon, Feb 22, 2010 at 6:13 AM, mirauta <bmirauta at 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 at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O