[R] Calculate Mean from List

Greg Snow Greg.Snow at imail.org
Tue Nov 9 18:41:49 CET 2010


You could use the "Reduce" function to get the sum of the matrices, then if there are no missing vales just divide by the number of matrices.  If there are missing values then you would probably need to use Reduce again to count the number of non-missing values.

Since all the matrices are the same dimensions you could also reformat the list into a 3 dimensional array and use the "apply" function to find the means.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Suphajak Ngamlak
> Sent: Tuesday, November 09, 2010 1:24 AM
> To: 'r-help at r-project.org'
> Subject: [R] Calculate Mean from List
> 
> Dear all,
> 
> I have a list of correlation coefficient matrixes. Each matrix
> represents one date.
> For example
> 
> A[[1]]
> 
>                 A             B             C
> A             1              0.2          0.3
> B             0.2          1              0.4
> C             0.3          0.4          1
> 
> A[[2]]
> 
>                 A             B             C
> A             1              0.5          0.6
> B             0.5          1              0.7
> C             0.6          0.7          1
> 
> ....
> 
> A[[n]]
> 
> I would like to calculate the mean of correlation coefficient from the
> whole time series, i.e.
> 
> Average cor(A,B) = (A[[1]][2,1] + A[[2]] [2,1] + ... + A[[n]] [2,1])/n
> Average cor(A,C) = (A[[1]][3,1] + A[[2]] [3,1] + ... + A[[n]] [3,1])/n
> Average cor(B,C) = (A[[1]][3,2] + A[[2]] [3,2] + ... + A[[n]] [3,2])/n
> 
> Please note that some cells are NA; so I need to remove them when
> calculating average.
> 
> How could I get this efficiently? Thank you
> 
> 
> Best Regards,
> Suphajak Ngamlak
> Equity and Derivatives Trading
> Phatra Securities Public Company Limited
> Tel: (66)2-305-9179
> Email: suphajak at phatrasecurities.com
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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