To calculate correlation between multiple variables in R, you can use cor() function. This function computes the correlation coefficient between variables in a data frame or matrix.
The following method shows how you can do it with syntax.
Method: Use cor() Function
cor(x,y)
The following examples shows how to use cor() function in R.
Use cor() Function to Calculate Correlation Between Two Variables
Let’s see how we can use cor() function to calculate correlation between two variables of data frame:
# Create data frame
df <- data.frame(Pressure1=c(78.2, 88.2, 71.7, 80.21, 84.21, 82.56, 72.12, 73.85),
Pressure2=c(12, 13, 11, 12, 14, 15, 13, 15),
Temperature=c(35, 36, 37, 38, 32, 30, 31, 34))
# Calculate correlation
c <- cor(df$Pressure1,df$Pressure2)
# Print correlation
print(c)
Output:
[1] 0.266023
As the output shows 0.266023 correlation between Pressure1 and Pressure2 columns of data frame.
Use cor() Function to Calculate Correlation for Entire Data Frame
Let’s see how we can use cor() function for entire data frame:
# Create data frame
df <- data.frame(Pressure1=c(78.2, 88.2, 71.7, 80.21, 84.21, 82.56, 72.12, 73.85),
Pressure2=c(12, 13, 11, 12, 14, 15, 13, 15),
Pressure3=c(25, 28, 29, 30, 32, 27, 29, 21),
Temperature=c(35, 36, 37, 38, 32, 30, 31, 34))
# Calculate correlation matrix
c <- cor(df)
# Print correlation matrix
print(c)
Output:
Pressure1 Pressure2 Pressure3 Temperature
Pressure1 1.00000000 0.2660230 0.3064410 -0.02750833
Pressure2 0.26602303 1.0000000 -0.3662667 -0.71384311
Pressure3 0.30644101 -0.3662667 1.0000000 0.02005080
Temperature -0.02750833 -0.7138431 0.0200508 1.00000000
Here the output shows correlation between all columns of data frame.