How to find the correlation matrix for rows of an R data frame?



To find the correlation matrix for rows of an R data frame, we can follow the below steps −

  • First of all, create a data frame.
  • Then, use cor function with transposed data frame to find the correlation matrix for rows.

Example1

Let's create a data frame as shown below −

 Live Demo

> x1<-rnorm(5)
> x2<-rnorm(5)
> x3<-rnorm(5)
> x4<-rnorm(5)
> x5<-rnorm(5)
> df1<-data.frame(x1,x2,x3,x4,x5)
> df1

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

      x1          x2       x3          x4       x5
1 0.23392099 -0.0919377 0.4623323 -0.5209734 0.1769501
2 0.66009528 0.8356888 -0.5394541 0.2082769 -0.1801216
3 -0.09660345 0.2935962 2.5287285 1.5602965 1.1191089
4 -0.23380750 0.3456661 0.1458570 -0.7058560 1.9192713
5 0.98615064 0.5303615 0.4312040 -0.5227473 1.8670896

Find the correlation matrix for rows

Using cor function to find the correlation matrix for rows in data frame after transposing the data frame −

 Live Demo

> x1<-rnorm(5)
> x2<-rnorm(5)
> x3<-rnorm(5)
> x4<-rnorm(5)
> x5<-rnorm(5)
> df1<-data.frame(x1,x2,x3,x4,x5)
> cor(t(df1[]))

Output

         [,1]       [,2]       [,3]       [,4]    [,5]
[1,] 1.0000000 -0.4225284 0.14255882 0.40937315 0.6203195
[2,] -0.4225284 1.0000000 -0.89215806 -0.31996808 -0.1164585
[3,] 0.1425588 -0.8921581 1.00000000 -0.01230975 -0.3128922
[4,] 0.4093732 -0.3199681 -0.01230975 1.00000000 0.8674166
[5,] 0.6203195 -0.1164585 -0.31289217 0.86741657 1.0000000

Example2

Let’s create a data frame as shown below −

 Live Demo

> y1<-rpois(5,1)
> y2<-rpois(5,2)
> y3<-rpois(5,5)
> y4<-rpois(5,2)
> y5<-rpois(5,10)
> df2<-data.frame(y1,y2,y3,y4,y5)
> df2

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

y1 y2 y3 y4 y5
1 1 2 8 2 16
2 1 4 9 4 15
3 0 2 3 3 15
4 0 2 3 2 8
5 0 2 5 2 9

Find the correlation matrix for rows

Using cor function to find the correlation matrix for rows in data frame after transposing the data frame −

 Live Demo

> y1<-rpois(5,1)
> y2<-rpois(5,2)
> y3<-rpois(5,5)
> y4<-rpois(5,2)
> y5<-rpois(5,10)
> df2<-data.frame(y1,y2,y3,y4,y5)
> cor(t(df2[]))

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 0.9856531 0.9330930 0.9594638 0.9848691
[2,] 0.9856531 1.0000000 0.9188314 0.9688966 0.9997316
[3,] 0.9330930 0.9188314 1.0000000 0.9818145 0.9262269
[4,] 0.9594638 0.9688966 0.9818145 1.0000000 0.9742043
[5,] 0.9848691 0.9997316 0.9262269 0.9742043 1.0000000

Example3

Let’s create a data frame as shown below −

 Live Demo

> z1<-runif(5,1,2)
> z2<-runif(5,1,5)
> z3<-runif(5,1,10)
> z4<-runif(5,2,10)
> z5<-runif(5,2,5)
> df3<-data.frame(z1,z2,z3,z4,z5)
> df3

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

z1 z2 z3 z4 z5
1 1.079122 3.430315 7.953942 6.142318 4.346613
2 1.400345 1.140915 7.714074 9.914491 2.362663
3 1.307579 1.557013 5.698641 6.810150 4.566523
4 1.452093 4.330748 4.710236 2.495453 3.531681
5 1.297150 1.628797 8.802337 5.099750 2.061406

Find the correlation matrix for rows

Using cor function to find the correlation matrix for rows in data frame after transposing the data frame −

 Live Demo

> z1<-runif(5,1,2)
> z2<-runif(5,1,5)
> z3<-runif(5,1,10)
> z4<-runif(5,2,10)
> z5<-runif(5,2,5)
> df3<-data.frame(z1,z2,z3,z4,z5)
> cor(t(df3[]))

Output

[,1] [,2] [,3] [,4] [,5]
[1,] 1.0000000 0.81635351 0.8600606 0.62241987 0.9144659
[2,] 0.8163535 1.00000000 0.9051244 0.09011508 0.8135256
[3,] 0.8600606 0.90512441 1.0000000 0.20627867 0.7375102
[4,] 0.6224199 0.09011508 0.2062787 1.00000000 0.4679658
[5,] 0.9144659 0.81352558 0.7375102 0.46796577 1.0000000
raja
Published on 03-Aug-2021 11:35:52
Advertisements