David Kane
2005-May-10 14:03 UTC
[R] summary statistics for lists of matrices or dataframes
Is there a simple way to calculate summary statistics for all the matrices or dataframes in a list? For example:> z <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = 2)) > z[[1]] [,1] [,2] [1,] 2 2 [2,] 2 2 [[2]] [,1] [,2] [1,] 4 4 [2,] 4 4>I would like to calculate, for example, the mean value for each cell. I can do that the hard way as:> (z[[1]] + z[[2]]) / 2[,1] [,2] [1,] 3 3 [2,] 3 3>But there must be an easier way. I am also interested in other statistics (like median and sd). Since all my matrices have the same attributes (especially row and column names), I would like to preserve those in the answer. Thanks, Dave Kane In case it matters:> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 1.0 year 2005 month 04 day 18 language R>
McGehee, Robert
2005-May-10 14:18 UTC
[R] summary statistics for lists of matrices or dataframes
You could try temporarily switching the list to an array, then just run an apply on rows and columns:> apply(array(do.call("cbind", z), c(dim(z[[1]]), length(z))), c(1, 2),mean) Best, Robert -----Original Message----- From: David Kane [mailto:dave at kanecap.com] Sent: Tuesday, May 10, 2005 10:04 AM To: r-help at stat.math.ethz.ch Subject: [R] summary statistics for lists of matrices or dataframes Is there a simple way to calculate summary statistics for all the matrices or dataframes in a list? For example:> z <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = 2)) > z[[1]] [,1] [,2] [1,] 2 2 [2,] 2 2 [[2]] [,1] [,2] [1,] 4 4 [2,] 4 4>I would like to calculate, for example, the mean value for each cell. I can do that the hard way as:> (z[[1]] + z[[2]]) / 2[,1] [,2] [1,] 3 3 [2,] 3 3>But there must be an easier way. I am also interested in other statistics (like median and sd). Since all my matrices have the same attributes (especially row and column names), I would like to preserve those in the answer. Thanks, Dave Kane In case it matters:> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 1.0 year 2005 month 04 day 18 language R>______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Dimitris Rizopoulos
2005-May-10 14:36 UTC
[R] summary statistics for lists of matrices or dataframes
Hi Dave, maybe you can find these functions useful: matSums <- function(lis){ out <- array(data=0., dim=dim(lis[[1]])) for(i in seq(along=lis)) out <- out + lis[[i]] out } ## matMeans <- function(lis) matSums(lis) / length(lis) ## matFun <- function(lis, FUN, ...){ if(!is.list(lis) || !all(sapply(lis, is.matrix))) stop("'lis' must be a list containing 2-dimensional arrays") dims <- sapply(lis, dim) n <- dims[1, 1] p <- dims[2, 1] if(!all(n==dims[1,]) || !all(p==dims[2,])) stop("the matrices must have the same dimensions") out <- apply(matrix(unlist(lis), n * p, length(lis)), 1, FUN, ...) dim(out) <- c(n, p) out } # The first two are faster than "matFun(lis, sum)" or "matFun(lis, mean)" # for large and many matrices ############# matFun <- function(lis, FUN, ...){ if(!is.list(lis) || !all(sapply(lis, is.matrix))) stop("'lis' must be a list containing 2-dimensional arrays") dims <- sapply(lis, dim) n <- dims[1, 1] p <- dims[2, 1] if(!all(n==dims[1,]) || !all(p==dims[2,])) stop("the matrices must have the same dimensions") out <- apply(matrix(unlist(lis), n*p, length(lis)), 1, FUN, ...) dim(out) <- c(n, p) out } ########### lis <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = 2), matrix(c(5,5,5,5), ncol=2)) matSums(lis) matFun(lis, sum) matMeans(lis) matFun(lis, mean) matFun(lis, median) matFun(lis, sd) Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "David Kane" <dave at kanecap.com> To: <r-help at stat.math.ethz.ch> Sent: Tuesday, May 10, 2005 4:03 PM Subject: [R] summary statistics for lists of matrices or dataframes> Is there a simple way to calculate summary statistics for all the > matrices or dataframes in a list? For example: > >> z <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = >> 2)) >> z > [[1]] > [,1] [,2] > [1,] 2 2 > [2,] 2 2 > > [[2]] > [,1] [,2] > [1,] 4 4 > [2,] 4 4 >> > > I would like to calculate, for example, the mean value for each > cell. I can do that the hard way as: > >> (z[[1]] + z[[2]]) / 2 > [,1] [,2] > [1,] 3 3 > [2,] 3 3 >> > > But there must be an easier way. I am also interested in other > statistics (like median and sd). Since all my matrices have the same > attributes (especially row and column names), I would like to > preserve > those in the answer. > > Thanks, > > Dave Kane > > In case it matters: > >> R.version > _ > platform i686-pc-linux-gnu > arch i686 > os linux-gnu > system i686, linux-gnu > status > major 2 > minor 1.0 > year 2005 > month 04 > day 18 > language R >> > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Huntsinger, Reid
2005-May-10 14:42 UTC
[R] summary statistics for lists of matrices or dataframes
In such a situation you could make the list into a 3-way array. For example,> d <- dim(z[[1]]) # all the same > n <- length(z) > z <- unlist(z) > dim(z) <- c(d,n)then you can get summary statistics for the (1,1) entries of the original z[[1]], z[[2]] etc however you would get summary statistics for the vector z[1,1,]. Moreover, you can vectorize this to get, say,> rowMeans(z, dims=2)[,1] [,2] [1,] 3 3 [2,] 3 3 and for arbitrary functions of the entries you can use apply():> apply(z, c(1,2), "median")Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of David Kane Sent: Tuesday, May 10, 2005 10:04 AM To: r-help at stat.math.ethz.ch Subject: [R] summary statistics for lists of matrices or dataframes Is there a simple way to calculate summary statistics for all the matrices or dataframes in a list? For example:> z <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = 2)) > z[[1]] [,1] [,2] [1,] 2 2 [2,] 2 2 [[2]] [,1] [,2] [1,] 4 4 [2,] 4 4>I would like to calculate, for example, the mean value for each cell. I can do that the hard way as:> (z[[1]] + z[[2]]) / 2[,1] [,2] [1,] 3 3 [2,] 3 3>But there must be an easier way. I am also interested in other statistics (like median and sd). Since all my matrices have the same attributes (especially row and column names), I would like to preserve those in the answer. Thanks, Dave Kane In case it matters:> R.version_ platform i686-pc-linux-gnu arch i686 os linux-gnu system i686, linux-gnu status major 2 minor 1.0 year 2005 month 04 day 18 language R>______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Patrick Burns
2005-May-10 14:49 UTC
[R] summary statistics for lists of matrices or dataframes
You could use 'do.call' with 'bind.array' (from S Poetry) or 'abind' to convert your list of matrices into a three-dimensional array. Patrick Burns Burns Statistics patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") David Kane wrote:>Is there a simple way to calculate summary statistics for all the >matrices or dataframes in a list? For example: > > > >>z <- list(matrix(c(2,2,2,2), ncol = 2), matrix(c(4,4,4,4), ncol = 2)) >>z >> >> >[[1]] > [,1] [,2] >[1,] 2 2 >[2,] 2 2 > >[[2]] > [,1] [,2] >[1,] 4 4 >[2,] 4 4 > > > >I would like to calculate, for example, the mean value for each >cell. I can do that the hard way as: > > > >>(z[[1]] + z[[2]]) / 2 >> >> > [,1] [,2] >[1,] 3 3 >[2,] 3 3 > > > >But there must be an easier way. I am also interested in other >statistics (like median and sd). Since all my matrices have the same >attributes (especially row and column names), I would like to preserve >those in the answer. > >Thanks, > >Dave Kane > >In case it matters: > > > >>R.version >> >> > _ >platform i686-pc-linux-gnu >arch i686 >os linux-gnu >system i686, linux-gnu >status >major 2 >minor 1.0 >year 2005 >month 04 >day 18 >language R > > > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > > >