To calculate spearman rank correlation in R, you can use cor.test() function with method argument.

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

Method 1: Use cor.test() Function

cor.test(vector1,vector2,method="spearman")


The following example shows how to calculate spearman rank correlation in R.

## Using cor.test() Function

Let’s see how we can calculate spearman rank correlation in R using cor.test() function:

# Create data frame
df <- data.frame(Machine_name=c("A","B","C","D","E","F","G","H"),
Pressure=c(12.39,11.25,12.15,13.48,13.78,12.89,12.21,12.58),
Temperature=c(78,89,85,84,81,79,77,85),
Status=c(TRUE,TRUE,FALSE,TRUE,FALSE,FALSE,TRUE,FALSE))

# Calculate Spearman Rank Correlation
x <- cor.test(df$Pressure,df$Temperature,method="spearman")

# Print Spearman Rank Correlation
print(x)


Output:

	Spearman's rank correlation rho

data:  df$Pressure and df$Temperature
S = 109.15, p-value = 0.4713
alternative hypothesis: true rho is not equal to 0
sample estimates:
rho
-0.2994066


Here the output shows spearman rank correlation between Pressure and Temperature column of dataframe.