Create a data.table object of combination of correlation coefficients.


To create a data.table object of combination of correlation coefficients, we first need to find the correlation matrix then the combination of variables for which the correlation matrix is created then data.table will be used to combine the combination of variables and the correlation coefficients.

Check out the below Examples to understand how it can be done.

Example 1

Following snippet creates a sample data frame −

x1<-rnorm(20)
x2<-rnorm(20,2,0.05)
x3<-rnorm(20,2,0.78)
x4<-rnorm(20,5,1)
df1<-data.frame(x1,x2,x3,x4)
df1

The following dataframe is created

           x1         x2       x3       x4
1   0.77994570 2.044288 1.2866822 6.139428
2  -0.57165011 1.931588 2.3060454 4.156558
3  -2.41470804 1.980485 2.0120719 5.928814
4  -1.25221041 2.059422 2.6341341 4.449050
5  -0.07042562 1.989987 0.6263381 4.378990
6   0.97821341 1.998165 2.4876630 3.473989
7  -0.99143292 1.980309 0.8280679 4.561764
8   0.98428356 2.046979 2.5538780 6.288159
9   0.32649034 1.987681 1.2260120 6.088381
10 -0.43238724 1.960947 1.5529283 4.425975
11  0.53374978 2.023177 2.7411385 6.853143
12  1.47847428 2.012680 2.2124862 5.552493
13 -1.27691863 1.977755 0.7198475 5.564577
14 -0.82864337 2.095960 1.9234651 4.903183
15  0.13440338 2.010327 2.6039556 3.557598
16  0.12110980 2.038554 1.3294818 4.130975
17 -0.03789286 1.971120 1.2343970 4.714649
18  1.60358705 1.930691 2.7022694 5.498787
19  0.22071213 1.976550 2.0997674 3.553371
20 -0.77764431 1.961341 0.8304568 5.664547

To find the correlation matrix for data stored in df1 on the above created data frame, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20,2,0.05)
x3<-rnorm(20,2,0.78)
x4<-rnorm(20,5,1)
df1<-data.frame(x1,x2,x3,x4)
Corr_M_df1<-cor(df1)
Corr_M_df1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

           x1         x2          x3          x4
x1 1.00000000 0.02822366  0.34313832  0.07829032
x2 0.02822366 1.00000000  0.17955473  0.13063972
x3 0.34313832 0.17955473  1.00000000 -0.03920003
x4 0.07829032 0.13063972 -0.03920003 1.00000000

To find the combination of variables in correlation matrix on the above created data frame, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20,2,0.05)
x3<-rnorm(20,2,0.78)
x4<-rnorm(20,5,1)
df1<-data.frame(x1,x2,x3,x4)
Corr_M_df1<-cor(df1)
Comb_Vars_df1<-t(combn(colnames(Corr_M_df1),2))
Comb_Vars_df1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1] [,2]
[1,] "x1" "x2"
[2,] "x1" "x3"
[3,] "x1" "x4"
[4,] "x2" "x3"
[5,] "x2" "x4"
[6,] "x3" "x4"

To load data.table package and create a data.table object for the combination of correlation coefficients on the above created data frame, add the following code to the above snippet −

x1<-rnorm(20)
x2<-rnorm(20,2,0.05)
x3<-rnorm(20,2,0.78)
x4<-rnorm(20,5,1)
df1<-data.frame(x1,x2,x3,x4)
Corr_M_df1<-cor(df1)
Comb_Vars_df1<-t(combn(colnames(Corr_M_df1),2))
library(data.table)
DT1<-data.table(Comb_Vars_df1,Corr_M_df1[Comb_Vars_df1])
DT1

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   V1 V2        V2
1: x1 x2 0.02822366
2: x1 x3 0.34313832
3: x1 x4 0.07829032
4: x2 x3 0.17955473
5: x2 x4 0.13063972
6: x3 x4 -0.03920003

Example 2

Following snippet creates a sample data frame −

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

The following dataframe is created

  y1  y2 y3 y4
1  0  2   4  0
2  1  1   3  0
3  0  5   5  1
4  2  0   3  2
5  1  0   5  3
6  0  0   5  1
7  1  1   8  1
8  0  1   3  4
9  0  1   2  1
10 1  3   8  1
11 1  1   4  3
12 1  2   2  3
13 1  1   2  4
14 0  2   8  3
15 1  2   8  2
16 1  2   6  4
17 3  1   4  2
18 3  2   3  0
19 0  1   5  4
20 1  3   5  4

To find the correlation matrix for data stored in df2 on the above created data frame, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,2)
y3<-rpois(20,5)
y4<-rpois(20,2)
df2<-data.frame(y1,y2,y3,y4)
Corr_M_df2<-cor(df2)
Corr_M_df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

            y1         y2         y3         y4
y1  1.0000000 -0.1405368 -0.15977673 -0.14617656
y2 -0.1405368  1.0000000  0.27579791 -0.11041245
y3 -0.1597767  0.2757979  1.00000000 -0.01662246
y4 -0.1461766 -0.1104125 -0.01662246  1.00000000

To find the combination of variables in correlation matrix on the above created data frame, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,2)
y3<-rpois(20,5)
y4<-rpois(20,2)
df2<-data.frame(y1,y2,y3,y4)
Corr_M_df2<-cor(df2)
Comb_Vars_df2<-t(combn(colnames(Corr_M_df2),2))
Comb_Vars_df2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

     [,1] [,2]
[1,] "y1" "y2"
[2,] "y1" "y3"
[3,] "y1" "y4"
[4,] "y2" "y3"
[5,] "y2" "y4"
[6,] "y3" "y4"

To create a data.table object for the combination of correlation coefficients on the above created data frame, add the following code to the above snippet −

y1<-rpois(20,1)
y2<-rpois(20,2)
y3<-rpois(20,5)
y4<-rpois(20,2)
df2<-data.frame(y1,y2,y3,y4)
Corr_M_df2<-cor(df2)
Comb_Vars_df2<-t(combn(colnames(Corr_M_df2),2))
DT2<-data.table(Comb_Vars_df2,Corr_M_df2[Comb_Vars_df2])
DT2

Output

If you execute all the above given snippets as a single program, it generates the following Output −

   V1 V2          V2
1: y1 y2 -0.14053680
2: y1 y3 -0.15977673
3: y1 y4 -0.14617656
4: y2 y3  0.27579791
5: y2 y4 -0.11041245
6: y3 y4 -0.01662246

Updated on: 28-Oct-2021

113 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements