- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to find the correlation of one variable with all the other variables in R?
To find the correlation of each variable with remaining variables, we can create a correlation matrix but for the correlation of only one variable with all the other variables we need to define the columns inside the cor function. The output will represent the columns and rows as passed inside the function.
Example1
Consider the below data frame −
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
Output
x1 x2 x3 x4 1 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
Finding the correlation of x1 with x2, x3, and x4 −
cor(as.matrix(df1[,1]),as.matrix(df1[,−1]))
Output
x2 x3 x4 [1,] −0.03973847 −0.02350932 −0.1992056
Example2
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
Output
y1 y2 y3 y4 y5 y6 1 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
Finding the correlation of y1 with remaining variables in df2 −
Example
cor(as.matrix(df2[,1]),as.matrix(df2[,−1]))
Output
y2 y3 y4 y5 y6 [1,] −0.1807339 0.2322878 −0.1330579 −0.1659442 −0.5139047
- Related Articles
- How to find the correlation matrix in R using all variables of a data frame?
- How to find the sequence of correlation between variables in an R data frame or matrix?
- How to create normal random variables with specific correlation between them in R?
- How to convert the correlation matrix into a data frame with combination of variables and their correlations in R?
- How to create correlation matrix plot without variables labels in R?
- How to find the group-wise correlation coefficient in R?
- How to find the significant correlation in an R data frame?
- How to find the correlation between corresponding columns of two matrices in R?
- How to find the correlation matrix with p-values for an R data frame?
- How to find the correlation coefficient between two data frames in R?
- Create scatterplot for two dependent variables and one independent variable in R.
- How to convert multiple numerical variables to factor variable in R?
- How to find the correlation coefficient between rows of two data frames in R?
- How to find the correlation matrix of groups for a data.table object in R?
- How to find the correlation matrix for rows of an R data frame?

Advertisements