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:
- ****+1**: A perfect positive monotonic relationship.
- 0: No monotonic relationship.
- **-1: A perfect negative monotonic relationship.
**Formula:
[\rho = 1 - \frac{6 \sum d_i^2}{n(n^2 - 1)}]
**Where:
- \rho is the Spearman Correlation coefficient
- d_i is the difference between the ranks of corresponding variables.
- n is the number of observations.
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.
- **cor: Calculates the correlation coefficient between two numeric vectors.
- **method: Specifies the type of correlation method (here, it is "spearman").
- **cat: Used to concatenate and print the final correlation value. R `
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.
- **cor.test: Performs a test of association or correlation between two numeric vectors.
- **method: Specifies the type of correlation method (here, it is "spearman"). R `
x = c(15, 18, 21, 15, 21) y = c(25, 25, 27, 27, 27) result = cor.test(x, y, method = "spearman") print(result)
`
**Output:

Output
- S is the value of the test statistic (S = 10.871)
- p-value is the significance level of the test statistic (p-value = 0.4397).
- alternative hypothesis is a character string describing the alternative hypothesis (true rho is not equal to 0).
- sample estimates is the correlation coefficient. For Spearman correlation coefficient it’s named as rho (Cor.coeff = 0.4564).