Reputation: 949
I have a correlation matrix, and using it to identify where variables are exactly correlated with other variables, knowing a correlation=1 is exact match. To better explain, here is some simulated data, to create some variables which are identical in values.
set.seed(123)
df <- data.frame(replicate(11,sample(1:1000,10,rep=TRUE)))
df <- cbind(df,df[,1:3],df[,1] )
names(df) <- c(paste0("X", 1:15))
cor_matrix <- round(cor(df, use="complete.obs", method="pearson"),2)
cor_matrix
> cor_matrix
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
X1 1.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 1.00 0.08 -0.69 1.00
X2 0.08 1.00 -0.11 0.15 -0.30 -0.41 0.05 0.09 -0.70 0.67 -0.10 0.08 1.00 -0.11 0.08
X3 -0.69 -0.11 1.00 -0.18 -0.05 0.29 -0.42 -0.10 -0.01 -0.12 0.03 -0.69 -0.11 1.00 -0.69
X4 0.10 0.15 -0.18 1.00 0.24 0.37 0.31 0.18 0.03 -0.09 0.28 0.10 0.15 -0.18 0.10
X5 -0.35 -0.30 -0.05 0.24 1.00 0.33 0.36 0.27 0.34 -0.08 -0.21 -0.35 -0.30 -0.05 -0.35
X6 -0.39 -0.41 0.29 0.37 0.33 1.00 -0.55 0.11 0.55 -0.78 0.15 -0.39 -0.41 0.29 -0.39
X7 0.26 0.05 -0.42 0.31 0.36 -0.55 1.00 0.01 -0.25 0.54 0.10 0.26 0.05 -0.42 0.26
X8 -0.17 0.09 -0.10 0.18 0.27 0.11 0.01 1.00 0.06 0.02 -0.47 -0.17 0.09 -0.10 -0.17
X9 0.06 -0.70 -0.01 0.03 0.34 0.55 -0.25 0.06 1.00 -0.82 -0.26 0.06 -0.70 -0.01 0.06
X10 -0.03 0.67 -0.12 -0.09 -0.08 -0.78 0.54 0.02 -0.82 1.00 -0.17 -0.03 0.67 -0.12 -0.03
X11 0.12 -0.10 0.03 0.28 -0.21 0.15 0.10 -0.47 -0.26 -0.17 1.00 0.12 -0.10 0.03 0.12
X12 1.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 1.00 0.08 -0.69 1.00
X13 0.08 1.00 -0.11 0.15 -0.30 -0.41 0.05 0.09 -0.70 0.67 -0.10 0.08 1.00 -0.11 0.08
X14 -0.69 -0.11 1.00 -0.18 -0.05 0.29 -0.42 -0.10 -0.01 -0.12 0.03 -0.69 -0.11 1.00 -0.69
X15 1.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 1.00 0.08 -0.69 1.00
As you can see correlation value of 1 can be found X1 with X12 and X15. X2 and X13, X3 AND X14. What I want to do is remove all values in the matrix that are not 1, (replace the unwanted values with zero) but will need to remove the 1s that are on the diagonal, where it's directly related to itself. Any help or ideas would be greatly appreciated. Many thanks
Upvotes: 2
Views: 1183
Reputation: 887118
We can assign the diag
elements to 0
diag(cor_matrix) <- 0
-output
cor_matrix
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15
X1 0.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 1.00 0.08 -0.69 1.00
X2 0.08 0.00 -0.11 0.15 -0.30 -0.41 0.05 0.09 -0.70 0.67 -0.10 0.08 1.00 -0.11 0.08
X3 -0.69 -0.11 0.00 -0.18 -0.05 0.29 -0.42 -0.10 -0.01 -0.12 0.03 -0.69 -0.11 1.00 -0.69
X4 0.10 0.15 -0.18 0.00 0.24 0.37 0.31 0.18 0.03 -0.09 0.28 0.10 0.15 -0.18 0.10
X5 -0.35 -0.30 -0.05 0.24 0.00 0.33 0.36 0.27 0.34 -0.08 -0.21 -0.35 -0.30 -0.05 -0.35
X6 -0.39 -0.41 0.29 0.37 0.33 0.00 -0.55 0.11 0.55 -0.78 0.15 -0.39 -0.41 0.29 -0.39
X7 0.26 0.05 -0.42 0.31 0.36 -0.55 0.00 0.01 -0.25 0.54 0.10 0.26 0.05 -0.42 0.26
X8 -0.17 0.09 -0.10 0.18 0.27 0.11 0.01 0.00 0.06 0.02 -0.47 -0.17 0.09 -0.10 -0.17
X9 0.06 -0.70 -0.01 0.03 0.34 0.55 -0.25 0.06 0.00 -0.82 -0.26 0.06 -0.70 -0.01 0.06
X10 -0.03 0.67 -0.12 -0.09 -0.08 -0.78 0.54 0.02 -0.82 0.00 -0.17 -0.03 0.67 -0.12 -0.03
X11 0.12 -0.10 0.03 0.28 -0.21 0.15 0.10 -0.47 -0.26 -0.17 0.00 0.12 -0.10 0.03 0.12
X12 1.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 0.00 0.08 -0.69 1.00
X13 0.08 1.00 -0.11 0.15 -0.30 -0.41 0.05 0.09 -0.70 0.67 -0.10 0.08 0.00 -0.11 0.08
X14 -0.69 -0.11 1.00 -0.18 -0.05 0.29 -0.42 -0.10 -0.01 -0.12 0.03 -0.69 -0.11 0.00 -0.69
X15 1.00 0.08 -0.69 0.10 -0.35 -0.39 0.26 -0.17 0.06 -0.03 0.12 1.00 0.08 -0.69 0.00
Upvotes: 3