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 <- cb
Works like a charm, thank you!
-Original Message-
From: Dimitris Rizopoulos [mailto:d.rizopou...@erasmusmc.nl]
Sent: Thursday, February 12, 2009 12:11 PM
To: Tan, Richard
Cc: r-help@r-project.org
Subject: Re: [R] get top 50 correlated item from a correlation matrix
for each item
a
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 Statis
3 matches
Mail list logo