A solution using a toy example

r <- cor(mvrnorm(1000,mu=rep(0,10),Sigma=diag(10)))  #assume a 10 x 10 
matrix

j <- i<-1:dim(r)[1] #generate matrix indices
lt <- outer(i,j,'>') #get boolean lower triangle
sort(r[lt],decreasing=TRUE)[1:5] #extract top 5 correlations



Joseph F. Lucke
Senior Statistician
Research Institute on Addictions
University at Buffalo
SUNY




"Tan, Richard" <r...@panagora.com> 
Sent by: r-help-boun...@r-project.org
02/12/2009 11:19 AM

To
<r-help@r-project.org>
cc

Subject
[R] get top 50 correlated item from a correlation matrix for each item






Hi,
 
I have a correlation matrix of about 3000 items, i.e., a 3000*3000
matrix.  For each of the 3000 items, I want to get the top 50 items that
have the highest correlation with it (excluding itself) and generate a
data frame with 3 columns like ("ID", "ID2", "cor"), where ID is those
3000 items each repeat 50 times, and ID2 is the top 50 correlated items
with ID, and cor is the correlation of ID and ID2.  I know I can use two
for loops to do it but it is very time consuming considering the
correlation matrix is generated for each month of the past 20 years.  Is
there a better way to do it?
 
Regards,
 
Richard 

                 [[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]]

______________________________________________
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.

Reply via email to