[R] Componentwise means of a list of matrices?

Stephan Kolassa Stephan.Kolassa at gmx.de
Tue Dec 30 16:13:25 CET 2008


Hi Phil,

thanks, that already helps: Reduce() gets me the means I need!

Unfortunately, Reduce() apparently won't help me with trimming or 
winsorizing the means, judging from its "successively" philosophy... Any 
other ideas out there?

Best,
Stephan


Phil Spector schrieb:
> Stephan -
>    If you try to apply mean directly to a matrix, it will just return a 
> scalar.  It's easy to get row means and column
> means, but to preserve each element of the matrix, I think
> it's better to do the computations directly:
> 
> Reduce('+',foo) / length(foo)
> 
>                                        - Phil Spector
>                      Statistical Computing Facility
>                      Department of Statistics
>                      UC Berkeley
>                      spector at stat.berkeley.edu
> 
> 
> On Tue, 30 Dec 2008, Stephan Kolassa wrote:
> 
>> Dear useRs,
>>
>> I have a list, each entry of which is a matrix of constant dimensions. 
>> Is there a good way (i.e., not using a for loop) to apply a mean to 
>> each matrix entry *across list entries*?
>>
>> Example:
>>
>> foo <- list(rbind(c(1,2,3),c(4,5,6)),rbind(c(7,8,9),c(10,11,12)))
>> some.sort.of.apply(foo,FUN=mean)
>>
>> I'm looking for a componentwise mean across the two entries of foo, 
>> i.e., the following output:
>>
>>     [,1] [,2] [,3]
>> [1,]    4    5    6
>> [2,]    7    8    9
>>
>> [NB. My "real" application involves trimming and psych::winsor(), so 
>> anything that generalizes to this would be extra good.]
>>
>> I've been looking at apply and {s,l,m,t}apply, by, with and aggregate 
>> and searched the list archives... any ideas?
>>
>> Thanks a lot,
>> Stephan
>>
>> ______________________________________________
>> 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.
>>
>



More information about the R-help mailing list