Dear All, Help is needed! I have a matrix with frequencies of fish larvae per length class (var. sl) and age-group (var. median.no) obtained with>k<-table(cut(sl,(5:22)),median.no) >k[2:5,1:5] #to ilustrate k4 5 6 7 (6,7] 3 1 0 0 (7,8] 3 0 1 0 (8,9] 3 4 3 5 (9,10] 3 15 7 13 from this matrix I would like to obtain the mean age per length class i.e. vector of line means. How can I do this? If, instead of the means I wanted the variances, how could I do it? Thanks in advance, Eduardo Esteves
> m <- matrix( 1:12, nc=3 ) > m[,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12> apply(m, 1, mean) # row means[1] 5 6 7 8> apply(m, 2, mean) # column means[1] 2.5 6.5 10.5 Replace 'mean' with 'var' if you want variances instead. On Thu, 2004-07-29 at 12:38, eesteves at ualg.pt wrote:> Dear All, > Help is needed! I have a matrix with frequencies of fish larvae per length > class (var. sl) and age-group (var. median.no) obtained with > > >k<-table(cut(sl,(5:22)),median.no) > >k[2:5,1:5] #to ilustrate k > > 4 5 6 7 > (6,7] 3 1 0 0 > (7,8] 3 0 1 0 > (8,9] 3 4 3 5 > (9,10] 3 15 7 13 > > from this matrix I would like to obtain the mean age per length class i.e. > vector of line means. How can I do this? If, instead of the means I wanted the > variances, how could I do it? > Thanks in advance, > Eduardo Esteves > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On Thu, 2004-07-29 at 12:38, eesteves at ualg.pt wrote:> Dear All, > Help is needed! I have a matrix with frequencies of fish larvae per length > class (var. sl) and age-group (var. median.no) obtained with > > >k<-table(cut(sl,(5:22)),median.no) > >k[2:5,1:5] #to ilustrate k > > 4 5 6 7 > (6,7] 3 1 0 0 > (7,8] 3 0 1 0 > (8,9] 3 4 3 5 > (9,10] 3 15 7 13 > > from this matrix I would like to obtain the mean age per length class i.e. > vector of line means. How can I do this? If, instead of the means I wanted the > variances, how could I do it? > Thanks in advance, > Eduardo Esteves >Ol?? Edu, To compute that kind of statistics it's better to have a vector with your length values (you can get it from the names of your matrix if you prefer). I supose you want to use the mean length interval so you can use the weigthed.mean function len <- seq(6.5,9.5,1) lenbar <- apply(k, 2, FUN=function(x){weighted.mean(len,x)}) to get variances you can use lenvar <- apply(k, 2, FUN=function(x){var(rep(len,x))}) I think you may also use cov.wt but I'm not sure. A more interesting result can be lendesc <- apply(k, 2, FUN=function(x){ vec <- rep(len,x) c(summary(vec), var=var(vec), mad=mad(vec)) }) I'm sure there are better and more elegant ways of doing this but here you have a "quick fix" for you're problem. Abra??os amigos EJ
For row or column means with a matrix of any size, you will be much better served by using rowMeans() colMeans() Best, Jim James W. MacDonald Affymetrix and cDNA Microarray Core University of Michigan Cancer Center 1500 E. Medical Center Drive 7410 CCGC Ann Arbor MI 48109 734-647-5623>>> Adaikalavan Ramasamy <ramasamy at cancer.org.uk> 07/29/04 09:04AM >>> > m <- matrix( 1:12, nc=3 ) > m[,1] [,2] [,3] [1,] 1 5 9 [2,] 2 6 10 [3,] 3 7 11 [4,] 4 8 12> apply(m, 1, mean) # row means[1] 5 6 7 8> apply(m, 2, mean) # column means[1] 2.5 6.5 10.5 Replace 'mean' with 'var' if you want variances instead. On Thu, 2004-07-29 at 12:38, eesteves at ualg.pt wrote:> Dear All, > Help is needed! I have a matrix with frequencies of fish larvae perlength> class (var. sl) and age-group (var. median.no) obtained with > > >k<-table(cut(sl,(5:22)),median.no) > >k[2:5,1:5] #to ilustrate k > > 4 5 6 7 > (6,7] 3 1 0 0 > (7,8] 3 0 1 0 > (8,9] 3 4 3 5 > (9,10] 3 15 7 13 > > from this matrix I would like to obtain the mean age per length classi.e.> vector of line means. How can I do this? If, instead of the means Iwanted the> variances, how could I do it? > Thanks in advance, > Eduardo Esteves > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide!http://www.R-project.org/posting-guide.html>______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html