如何找到R中一个变量与所有其他变量的相关性?
为了找到每个变量与剩余变量的相关性,我们可以创建一个相关矩阵,但是对于仅一个变量与所有其他变量的相关性,我们需要在cor函数内定义列。输出将表示在函数内部传递的列和行。
例1
考虑以下数据帧-
x1<−rnorm(20)输出结果x2<−rnorm(20,5)
x3<−rnorm(20,1)
x4<−rnorm(20,1,0.004)
df1<−data.frame(x1,x2,x3,x4)
df1
x1 x2 x3 x41 0.218668324 5.259419 −0.729656992 0.9970744
2 −0.449469183 3.988644 1.643049356 1.0019346
3 −1.320063760 4.645783 0.945824833 1.0002887
4 0.900912365 4.915662 0.281402826 0.9997605
5 −1.277076293 4.106256 0.209859279 1.0020822
6 0.208085001 6.148850 1.202134387 1.0029056
7 0.168713334 5.065017 −0.747802047 1.0043622
8 −1.520794265 4.296433 0.798054984 1.0059425
9 0.650796875 3.454918 −0.830742647 1.0009890
10 −0.001671951 5.115431 0.898794442 0.9961067
11 −0.453247780 4.329456 1.766517821 0.9925434
12 −1.116829477 4.277810 0.404241362 1.0013094
13 −1.740383204 5.832924 0.502051096 0.9960360
14 2.056005104 4.146595 1.938634058 1.0015199
15 0.180966542 3.897272 1.229711042 1.0004884
16 −0.541882877 5.711087 2.339226199 0.9962318
17 −1.297807469 4.744936 0.006237027 1.0019933
18 −0.597723769 5.621779 1.213939466 1.0029180
19 −1.247040400 5.014155 0.453961227 0.9979829
20 1.114721291 5.809178 −0.161838355 0.9876081
找到x1与x2,x3和x4的相关性-
cor(as.matrix(df1[,1]),as.matrix(df1[,−1]))输出结果
x2 x3 x4[1,] −0.03973847 −0.02350932 −0.1992056
例2
y1<−rpois(20,5)输出结果y2<−rpois(20,1)
y3<−rpois(20,2)
y4<−rpois(20,10)
y5<−rpois(20,2)
y6<−rpois(20,4)
df2<−data.frame(y1,y2,y3,y4,y5,y6)
df2
y1 y2 y3 y4 y5 y61 6 1 3 16 1 3
2 6 1 2 8 3 5
3 6 0 1 9 3 3
4 8 0 3 10 1 3
5 4 2 6 10 3 2
6 4 1 3 9 3 6
7 7 1 3 10 3 4
8 2 2 0 3 4 5
9 1 1 1 11 0 5
10 7 0 5 9 1 4
11 7 0 6 7 0 3
12 4 1 4 11 2 4
13 9 3 2 6 1 3
14 5 0 3 6 1 6
15 6 3 6 11 2 3
16 6 2 4 11 3 5
17 6 1 7 8 1 4
18 3 1 6 14 1 7
19 3 4 2 13 2 5
20 5 0 3 7 2 6
找到y1与df2中剩余变量的相关性-
示例
cor(as.matrix(df2[,1]),as.matrix(df2[,−1]))输出结果
y2 y3 y4 y5 y6[1,] −0.1807339 0.2322878 −0.1330579 −0.1659442 −0.5139047
以上是 如何找到R中一个变量与所有其他变量的相关性? 的全部内容, 来源链接: utcz.com/z/314717.html