[R] Calculate Mean from List

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Nov 9 09:38:47 CET 2010


one way is the following:

A <- replicate(10, cor(matrix(rnorm(30), 10, 3)), simplify = FALSE)

triA <- sapply(A, function (m) m[upper.tri(m)])
rowMeans(triA, na.rm = TRUE)


I hope it helps.

Best,
Dimitris


On 11/9/2010 9:23 AM, Suphajak Ngamlak wrote:
> 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.
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/



More information about the R-help mailing list