如何根据R中的相关系数将相关矩阵转换为逻辑矩阵?
要将相关矩阵转换为基于R中相关系数的逻辑矩阵,我们可以按照以下步骤操作-
首先,创建一个矩阵。
然后,找到相关矩阵。
之后,使用大于或小于符号将相关矩阵转换为基于系数值的逻辑矩阵。
示例1
让我们创建一个矩阵,如下所示-
M1<-matrix(sample(1:100,100),ncol=4) M1
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
输出结果
[,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
找到相关矩阵
使用cor函数查找相关矩阵-
M1<-matrix(sample(1:100,100),ncol=4) Cor_M1<-cor(M1) Cor_M1输出结果
[,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
根据相关系数将相关矩阵转换为逻辑矩阵
使用相关系数值0.15将Cor_M1转换为具有TRUE的逻辑矩阵,其中相关系数大于0.15-
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输出结果
[,1] [,2] [,3] [,4] [1,] TRUE FALSE TRUE FALSE [2,] FALSE TRUE TRUE FALSE [3,] TRUE TRUE TRUE FALSE [4,] FALSE FALSE FALSE TRUE
例2
让我们创建一个矩阵,如下所示-
M2<-matrix(round(rnorm(100),1),ncol=4) M2
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
输出结果
[,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
找到相关矩阵
使用cor函数查找相关矩阵-
M2<-matrix(round(rnorm(100),1),ncol=4) Cor_M2<-cor(M2) Cor_M2输出结果
[,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
根据相关系数将相关矩阵转换为逻辑矩阵
使用相关系数值0.25将Cor_M2转换为具有TRUE的逻辑矩阵,其中相关系数大于0.25-
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输出结果
[,1] [,2] [,3] [,4] [1,] TRUE FALSE FALSE FALSE [2,] FALSE TRUE FALSE FALSE [3,] FALSE FALSE TRUE TRUE [4,] FALSE FALSE TRUE TRUE