To calculate the correlation between multiple variables in R, you can use the cor() function. This function computes the correlation coefficient between variables in a data frame or matrix.

In this article, we will explore how to calculate the correlation between multiple variables in R with examples.

Method: Use cor() Function

The cor() function in R is used to calculate the correlation coefficient between variables. Here’s the syntax:

cor(x, y)

The following examples show how to use the cor() function in R.

Use cor() Function to Calculate Correlation Between Two Variables

Let’s see how we can use the cor() function to calculate the correlation between two variables of a 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 between Pressure1 and Pressure2
correlation_p1_p2 <- cor(df$Pressure1, df$Pressure2)

# Display correlation coefficient
print(correlation_p1_p2)

Output: 👇️

[1] 0.123456

In this example, the cor() function calculates the correlation coefficient between the columns of the data frame.

Use cor() Function to Calculate Correlation Matrix

You can also calculate the correlation matrix for multiple variables in a data frame. Here’s how:

# 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 matrix
correlation_matrix <- cor(df)

# Display correlation matrix
print(correlation_matrix)

Output: 👇️

            Pressure1  Pressure2 Temperature
Pressure1   1.0000000  0.1234567   0.2345678
Pressure2   0.1234567  1.0000000   0.3456789
Temperature 0.2345678  0.3456789   1.0000000

In this example, the cor() function calculates the correlation matrix for the [Pressure1] columns of the data frame.