Spearman Correlation Testing in R Programming (original) (raw)

Last Updated : 28 Jul, 2025

Spearman Correlation Testing in R programming is a statistical method used to evaluate the strength and direction of a monotonic relationship between two ranked variables. Unlike Pearson correlation, it does not assume normal distribution or linearity, making it ideal for ordinal data and non-linear associations.

Spearman’s correlation, often denoted as Spearman’s rho (\rho), measures the strength and direction of the monotonic relationship between two ranked variables. It ranges from -1 to +1:

**Formula:

[\rho = 1 - \frac{6 \sum d_i^2}{n(n^2 - 1)}]

**Where:

Implementation of Spearman Correlation Testing in R

We calculate the Spearman correlation using two methods in R programming language.

1. Calculating Spearman Correlation Using cor() Function

We calculate the Spearman correlation coefficient between two numeric vectors using **cor() to get only the correlation coefficient.

x = c(15, 18, 21, 15, 21) y = c(25, 25, 27, 27, 27) result = cor(x, y, method = "spearman") cat("Spearman correlation coefficient is:", result)

`

**Output:

Spearman correlation coefficient is: 0.4564355

2. Calculating Spearman Correlation Using cor.test() Function

We compute the Spearman correlation coefficient using cor.test() to get both the coefficient and p-value for hypothesis testing.

x = c(15, 18, 21, 15, 21) y = c(25, 25, 27, 27, 27) result = cor.test(x, y, method = "spearman") print(result)

`

**Output:

spearman_correlation

Output