To calculate partial correlation in R, you can use pcor() function from ppcor package. The partial correlation is used to calculate correlation between two columns of data frame while controlling third column of data frame.

The following method shows how you can do it with syntax.

Method: Use pcor() Function

library(ppcor)

p <- pcor(df)


The following example shows how to calculate partial corrlation in R.

## Use pcor() to Calculate Partial Correlation

Let’s see how we can calculate partial correlation of data frame:

# Import library
library(ppcor)

# Create data frame
df <- data.frame(Pressure1=c(78.2, 88.2, 71.7, 80.21, 84.21, 82.56, 72.12, 73.85),
Velocity=c(25, 28, 29, 30, 32, 27, 29, 21),
Temperature=c(35, 36, 37, 38, 32, 30, 31, 34))

# Calculate partial correlation
p <- pcor(df)

# Show partial correlation
print(p)


Output:

$estimate Pressure1 Velocity Temperature Pressure1 1.0000000 0.30717055 -0.03536070 Velocity 0.3071705 1.00000000 0.02993127 Temperature -0.0353607 0.02993127 1.00000000$p.value
Pressure1  Velocity Temperature
Pressure1   0.0000000 0.5027801   0.9400073
Velocity    0.5027801 0.0000000   0.9492099
Temperature 0.9400073 0.9492099   0.0000000

$statistic Pressure1 Velocity Temperature Pressure1 0.00000000 0.72174748 -0.07911842 Velocity 0.72174748 0.00000000 0.06695836 Temperature -0.07911842 0.06695836 0.00000000$n
[1] 8

$gp [1] 1$method
[1] "pearson"


The output shows partial correlation of data frame which calculated using pcor() function.