How to convert a correlation matrix into a logical matrix based on correlation coefficient in R?



To convert a correlation matrix into a logical matrix based on correlation coefficient in R, we can follow the below steps −

  • First of all, create a matrix.

  • Then, find the correlation matrix.

  • After that, convert the correlation matrix into logical matrix based on coefficient value using greater than or less than sign.

Example 1

Let’s create a matrix as shown below −

 Live Demo

M1<-matrix(sample(1:100,100),ncol=4) M1

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

   [,1] [,2] [,3] [,4] [1,] 91 22 61 37 [2,] 33 89 49 9 [3,] 71 6 36 13 [4,] 51 18 57 90 [5,] 41 23 4 54 [6,] 30 64 77 43 [7,] 67 12 65 7 [8,] 80 47 97 2 [9,] 34 32 21 79 [10,] 24 73 17 25 [11,] 66 38 45 58 [12,] 52 53 92 63 [13,] 31 74 76 100 [14,] 95 69 82 35 [15,] 27 60 98 59 [16,] 50 44 55 16 [17,] 29 88 11 81 [18,] 28 14 99 72 [19,] 1 68 15 78 [20,] 5 46 86 84 [21,] 70 10 19 93 [22,] 3 94 87 20 [23,] 56 39 75 62 [24,] 26 40 8 42 [25,] 85 83 96 48

Find the correlation matrix

Using cor function to find the correlation matrix −

 Live Demo

M1<-matrix(sample(1:100,100),ncol=4) Cor_M1<-cor(M1) Cor_M1

Output

        [,1]       [,2]       [,3]       [,4] [1,] 1.0000000 -0.35856139 0.1834276 -0.29261479 [2,] -0.3585614 1.00000000 0.1613665 -0.08473756 [3,] 0.1834276 0.16136649 1.0000000 -0.12695909 [4,] -0.2926148 -0.08473756 -0.1269591 1.00000000

Convert the correlation matrix into logical matrix based on correlation coefficient

Using correlation coefficient value 0.15 to convert Cor_M1 into logical matrix with TRUE where correlation coefficient is greater than 0.15 −

 Live Demo

M1<-matrix(sample(1:100,100),ncol=4) Cor_M1<-cor(M1) Cor_M1_Above_0.15 <- Cor_M1>0.15 Cor_M1_Above_0.15

Output

    [,1]  [,2] [,3] [,4] [1,] TRUE FALSE TRUE FALSE [2,] FALSE TRUE TRUE FALSE [3,] TRUE TRUE TRUE FALSE [4,] FALSE FALSE FALSE TRUE

Example2

Let’s create a matrix as shown below −

 Live Demo

M2<-matrix(round(rnorm(100),1),ncol=4) M2

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

Output

       [,1] [,2] [,3] [,4] [1,]   0.4 -0.6 -1.0 -0.6 [2,]   1.2 -0.4  0.4 1.2 [3,]   -0.2 1.0 -0.5 0.9 [4,]   0.0 -1.7 -1.6 -2.1 [5,]  -0.4 -1.6  0.3 0.7 [6,]  -0.4 -1.4  1.7 0.0 [7,]   1.1 -0.3  0.3 0.6 [8,]   0.2 -0.2 -0.4 -0.3 [9,]   0.2  0.9 -0.7 0.2 [10,]  0.6 -0.7  0.1 -0.1 [11,] -1.8 -0.7  0.3 0.3 [12,]  0.4 -0.6 -1.5 0.8 [13,] -0.1  0.7  0.4 0.1 [14,]  0.6 -0.5 -0.2 -1.2 [15,] -0.2  0.1 -0.6 0.1 [16,]  0.6 -0.3  0.8 -1.5 [17,] -0.4  0.6 -0.8 -0.3 [18,] -0.5 -2.1 -2.5 -1.2 [19,] -0.8  0.5  1.6 0.1 [20,]  0.2 -2.3  1.2 1.1 [21,] -0.2  0.4 -0.2 -1.5 [22,]  0.3 -1.2 -0.2 -2.1 [23,]  0.5 -0.6  1.1 -0.2 [24,] -0.4  0.9  0.7 -0.8 [25,] -0.1  0.6  0.2 -0.2

Find the correlation matrix

Using cor function to find the correlation matrix −

 Live Demo

M2<-matrix(round(rnorm(100),1),ncol=4) Cor_M2<-cor(M2) Cor_M2

Output

         [,1]       [,2]       [,3]       [,4] [1,] 1.00000000 -0.0548232 -0.02367749 0.02923548 [2,] -0.05482320 1.0000000 0.10940827 0.11996196 [3,] -0.02367749 0.1094083 1.00000000 0.29432660 [4,] 0.02923548 0.1199620 0.29432660 1.00000000

Convert the correlation matrix into logical matrix based on correlation coefficient

Using correlation coefficient value 0.25 to convert Cor_M2 into logical matrix with TRUE where correlation coefficient is greater than 0.25 −

 Live Demo

M2<-matrix(round(rnorm(100),1),ncol=4) Cor_M2<-cor(M2) Cor_M2_Above_0.25 <- Cor_M2>0.25 Cor_M2_Above_0.25

Output

     [,1] [,2]  [,3]  [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE TRUE [4,] FALSE FALSE TRUE TRUE
Updated on: 2021-08-11T08:52:15+05:30

279 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements