How to get correlation matrix for a set of variables in R

How to get correlation matrix for a set of variables in R


  R Interview Questions

Correlation coefficient between 2 variables ( or any number of variables ) can be found using the cor function.

For example, if you want to find out the correlation coefficient between the sepal’s length and width of the iris data set, use

> cor(iris[1:2],)
             Sepal.Length Sepal.Width
Sepal.Length    1.0000000  -0.1175698
Sepal.Width    -0.1175698   1.0000000

You can use the cor () function to find out the correlation matrix ( a matrix of correlation coefficients ) across all the variables in the iris data set as well.

> cor(iris[1:4])
             Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length    1.0000000  -0.1175698    0.8717538   0.8179411
Sepal.Width    -0.1175698   1.0000000   -0.4284401  -0.3661259
Petal.Length    0.8717538  -0.4284401    1.0000000   0.9628654
Petal.Width     0.8179411  -0.3661259    0.9628654   1.0000000

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: