Skip to content
Prev 170287 / 398503 Next

get top 50 correlated item from a correlation matrix for each item

Works like a charm, thank you! 

-----Original Message-----
From: Dimitris Rizopoulos [mailto:d.rizopoulos at erasmusmc.nl] 
Sent: Thursday, February 12, 2009 12:11 PM
To: Tan, Richard
Cc: r-help at r-project.org
Subject: Re: [R] get top 50 correlated item from a correlation matrix
for each item

a possible vectorized solution is the following:

cor.mat <- cor(matrix(rnorm(100*1000), 1000, 100)) p <- 30 # how many
top items

n <- ncol(cor.mat)
cmat <- col(cor.mat)
ind <- order(-cmat, cor.mat, decreasing = TRUE) - (n * cmat - n)
dim(ind) <- dim(cor.mat)
ind <- ind[seq(2, p + 1), ]
out <- cbind(ID = c(col(ind)), ID2 = c(ind)) as.data.frame(cbind(out,
cor = cor.mat[out]))


I hope it helps.

Best,
Dimitris
Tan, Richard wrote:
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014