Re: [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 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. > -- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O __ 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.
Re: [R] how do I calculate means or cov matrix for multivariate groups
Thanks, These functions solve 1 problem ( the mean). I tried to use them also for the cov matrix but I didn't succed. Is there any way to calculate the cov matrix for groups? -- View this message in context: http://n4.nabble.com/how-do-I-calculate-means-or-cov-matrix-for-multivariate-groups-tp1564157p1564234.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.
Re: [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 x2.5 2.0 2 y8.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 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 > 1015 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]] __ 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.
[R] how do I calculate means or cov matrix for multivariate groups
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 1015 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.