Skip to content
Prev 170284 / 398503 Next

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: