[R] column names of a correlation matrix

Tony Plate tplate at acm.org
Wed Oct 28 19:03:04 CET 2009


Here's a simple example that might help you get what you want:

> set.seed(1)
> x <- matrix(rnorm(30), ncol=3, dimnames=list(NULL, letters[1:3]))
> (xc <- cor(x))
           a          b          c
a  1.0000000 -0.3767034 -0.7158385
b -0.3767034  1.0000000  0.6040273
c -0.7158385  0.6040273  1.0000000
> (cd <- data.frame(elt=outer(colnames(xc), colnames(xc), paste, sep=":")[upper.tri(xc)], row=row(xc)[upper.tri(xc)], col=col(xc)[upper.tri(xc)], cor=xc[upper.tri(xc)]))
  elt row col        cor
1 a:b   1   2 -0.3767034
2 a:c   1   3 -0.7158385
3 b:c   2   3  0.6040273
> cd[order(-cd$cor),]
  elt row col        cor
3 b:c   2   3  0.6040273
1 a:b   1   2 -0.3767034
2 a:c   1   3 -0.7158385
> 

If you need something more efficient, try using which(..., arr.ind) to pick out matrix style indices, e.g.:
> (ii <- which(xc > -0.4 & upper.tri(xc), arr.ind=T))
  row col
a   1   2
b   2   3
> cbind(ii, cor=xc[ii])
  row col        cor
a   1   2 -0.3767034
b   2   3  0.6040273
> 

-- Tony Plate

Lee William wrote:
> Hi! All,
> I am working on a correlation matrix of 4217x4217 named 'cor_expN'. I wish
> to obtain pairs with highest correlation values. So, I did this
>> b=matrix(data=NA,nrow=4217,ncol=1)
>> rownames(b)=rownames(cor_expN)
>> for(i in 1:4217){b[i,]=max(cor_expN[i,])}
>> head(b)
>                    [,1]
> aaeA_b3241_14 0.7181912
> aaeB_b3240_15 0.7513084
> aaeR_b3243_15 0.7681684
> aaeX_b3242_12 0.5230587
> aas_b2836_14   0.6615927
> aat_b0885_14    0.6344144
> 
> Now I want the corresponding columns for the above values. For that I tried
> this
>> c=matrix(data=NA,nrow=4217,ncol=1)
>> for(i in 1:4217){b[i,]=colnames(max(cor_expN[i,]))}
> 
> And got the following error:
> Error in b[i, ] = colnames(max(cor_expN[i, ])) : number of items to replace
> is not a multiple of replacement length
> Any thoughts?
> 
> Lee
> 
> 	[[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