I use the following method to calculate a correlation of my dataset:
cor( var1, var2, method = "method")
But I like to create a correlation matrix of 4 different variables. What's the easiest way to do this?
I use the following method to calculate a correlation of my dataset:
cor( var1, var2, method = "method")
But I like to create a correlation matrix of 4 different variables. What's the easiest way to do this?
You can also calculate correlations for all variables but exclude selected ones, for example:
Also, to calculate correlation between each variable and one column you can use
sapply()
See
corr.test
function inpsych
package:And yet another shameless self-advert: https://gist.github.com/887249
You might want to look at Quick-R, which has a lot of nice little tutorials on how you can do basic statistics in R. For example on correlations:
http://www.statmethods.net/stats/correlations.html
If you would like to combine the matrix with some visualisations I can recommend (I am using the built in
iris
dataset):The Performance Analytics basically does the same but includes significance indicators by default.
Or this nice and simple visualisation:
Use the same function (
cor
) on a data frame, e.g.:Or, on a data frame also holding discrete variables, (also sometimes referred to as factors), try something like the following: