Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
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
Advertisements
