[R] summary statistics for lists of matrices or dataframes

Huntsinger, Reid reid_huntsinger at merck.com
Tue May 10 16:42:06 CEST 2005


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




More information about the R-help mailing list