Amanda,

If I understand what you're trying to do, this example might help you.

M <- structure(c(0.66, 0.05, -0.93, -0.61, 0.65, -0.25, 0.23, -0.89,
 0.37, 0.38, -0.91, 0.91, -0.05, -0.65, -0.94, 0.73, -0.88, 0.25,
0.04, -0.89, -0.47, -0.46, 0.86, -0.29, 0.92, 0.22, 0.77, -0.98,
 -0.56, 0.11, 0.35, -0.49, 0.48, 0.42, -0.28, 0.16, 0.2, 0.61,
0.7, 0.94, 0.89, 0.46, 0.93, 0.36, -0.95, -0.01, 0.27, 0.73,
 -0.17, -0.93, 0.36, 0.78, 0.02, -0.8, -0.57, -0.03, -0.81, -0.74,
0.16, 0.52, 0.65, -0.45, 0.46, -0.68, -0.85, 0.74, 0.77, -0.39,
 0.03, 0.93, -0.23, -0.91, -0.14, 0.77, -0.36, 0.43, -0.5, 0.5,
-0.68, -0.6, 0.12, -0.18, 0.16, -0.38, -0.18, -0.59, -0.3, -0.14,
 0.53, -0.53, -0.56, 0.14, -0.54, 0.93, 0.54, 0.03, 0.06, 0.14,
0.97, -0.09), .Dim = c(10L, 10L), .Dimnames = list(c("D", "C",
 "D", "C", "B", "A", "C", "A", "C", "B"), c("D", "C", "D", "C",
 "B", "A", "C", "A", "C", "B")))

meanM <- data.frame(row=NA, col=NA, mean=NA)

count <- 0
for(i in 1:(ng-1)) {
for(j in (i+1):ng) {
count <- count + 1
meanM[count, "row"] <- groups[i]
 meanM[count, "col"] <- groups[j]
meanM[count, "mean"] <- mean(M[dimnames(M)[[1]]==groups[i],
dimnames(M)[[2]]==groups[j]])
 }}

Jean




On Mon, Sep 30, 2013 at 9:55 AM, Amanda Li <amandali@uchicago.edu> wrote:

> Hello,
>
> I want to do pairwise calculation, but I am not sure how to do so.
>
> i.e. I have a correlation matrix M 200*200. Namely colnames(M)=rownames(M).
> In addition, colnames(M) is one of A, B, C, D. I want to first sort the
> matrix M into 16 modules according to colnames and rownames, and then
> apply:
> avecor <- function(x,y) {
> z <- (sum(cor(x,y)*cor(x,y))/length(cor(x,y)))^0.5
> return(z)
> }
>
> So as to calculate the average correlation between A,B; A,C; A,D; B,C; B,D;
> C,D.
>
> Thanks in advance for your help!
>
> Best,
> Amanda
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

