I have a correlation matrix in R, and I want to know how many groups (and place these groups in vectors) of the elements are more than 95% correlated between them.
X <- matrix(0,3,5)
X[,1] <- c(1,2,3)
X[,2] <- c(1,2.2,3)*2
X[,3] <- c(1,2,3.3)*3
X[,4] <- c(6,5,1)
X[,5] <- c(6.1,5,1.2)*4
cor.matrix <- cor(X)
cor.matrix <- cor.matrix*lower.tri(cor.matrix)
cor.vector <- which(cor.matrix>0.95, arr.ind=TRUE)
cor.vector then contains:
row col
[1,] 2 1
[2,] 3 1
[3,] 3 2
[4,] 5 4
This means that, as expected, the correlations between the vectors 1,2 and 3, as well as 4 and 5.
I need to get two vectors c(1,2,3)and c(4,5)as the final result.
This is a simple example, however I am processing large matrices.