Statistics

Correlation Coefficient Calculator

Paste two columns of numbers and get Pearson’s r and Spearman’s rank correlation side by side, each with a significance test, plus a confidence interval for r, a scatter plot, a rank table and every step of the calculation.

Your data

Copy two columns from Excel or Google Sheets and paste. Tabs, commas, semicolons or spaces between the values all work. A header row is picked up as the variable names.

Paste your data and press Calculate, or load the example to see how it works.

What this calculator does

A correlation coefficient is a single number between −1 and +1 that describes how closely two variables move together. Positive values mean they rise together, negative values mean one falls as the other rises, and values near zero mean there is no consistent pattern.

The calculator returns two coefficients. Pearson’s r measures how well the points follow a straight line. Spearman’s ρ (rho) measures how consistently the relationship goes in one direction, whether or not it is straight. For each one you get a t-test of whether the true correlation could be zero. For r you also get a confidence interval.

Pearson or Spearman: which one to report

  • Use Pearson for two continuous measurements where you expect a straight-line relationship, such as height and weight or ad spend and clicks.
  • Use Spearman when the relationship curves but keeps the same direction, when a few extreme values could distort the result, or when the data is ordinal, such as survey ratings or class ranks.
  • When the two disagree, look at the scatter plot. A Spearman value well above Pearson usually means a curved but steady relationship. A Pearson value well above Spearman usually means one or two extreme points are creating most of the correlation.

The formulas

Pearson’s r divides how much X and Y vary together by how much each varies on its own:

r = Sxy / √(Sxx · Syy)
Sxy = Σ(x − x̄)(y − ȳ)   ·   Sxx = Σ(x − x̄)²   ·   Syy = Σ(y − ȳ)²

Spearman’s ρ replaces every value with its rank (tied values share the average of their ranks) and then applies the same formula to the ranks. When there are no ties this reduces to the shortcut you may have seen in textbooks, where d is the difference between the two ranks of each pair:

ρ = 1 − 6Σd² / (n(n² − 1))

Both coefficients are tested with t = r√(n − 2) / √(1 − r²) on n − 2 degrees of freedom. The confidence interval for Pearson’s r uses Fisher’s z transformation, z = artanh(r), whose standard error is 1 / √(n − 3).

Worked example: experience and salary

Press Load example to use this data: years of experience and annual salary in ₹ LPA for 12 data analysts in Bengaluru, from freshers at ₹3.5 LPA to a 15-year analytics lead at ₹42 LPA.

  • Pearson r = 0.9396 (p < 0.0001, 95% CI 0.7937 to 0.9833). Strong, and squaring it gives R² = 0.8829.
  • Spearman ρ = 0.9983 (p < 0.0001). Almost perfect: every extra year of experience comes with a higher salary.

Why the gap? Salary does not rise in a straight line. It climbs slowly in the early years and much faster later, so the points bend upward and a straight line misses the top end. Spearman only asks whether the order matches, which it almost exactly does. If you take the log of salary first, the curve straightens and Pearson’s r rises to 0.9990. That is the practical signal: a large gap between the two coefficients tells you a transformation or a non-linear model may suit the data better than a straight line.

Reading the result

  • Sign gives the direction. Size gives the strength. A correlation of −0.8 is just as strong as +0.8.
  • The p-value tests whether the true correlation could be zero. With a large sample, even a tiny correlation such as 0.05 can be significant without being useful.
  • The confidence interval is often more informative than the p-value. An r of 0.6 with an interval of 0.1 to 0.85 says the data is consistent with anything from weak to very strong.
  • Correlation is not causation. Ice cream sales and AC sales in Chennai rise together because of summer, not because one drives the other. See causal inference vs correlation for how analysts separate the two.

Getting the same result in Excel and Python

In Excel, =CORREL(A2:A13, B2:B13) returns Pearson’s r. For Spearman, rank each column with =RANK.AVG(A2, $A$2:$A$13, 1) in helper columns and run CORREL on the ranks. In Python:

from scipy import stats

experience = [0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15]
salary_lpa = [3.5, 4.2, 4.0, 5.1, 6.0, 7.2, 8.5, 9.8, 13, 18, 24, 42]

pr = stats.pearsonr(experience, salary_lpa)
print(pr.statistic, pr.pvalue, pr.confidence_interval())   # 0.9396, 5.7e-06, (0.794, 0.983)

sr = stats.spearmanr(experience, salary_lpa)
print(sr.statistic, sr.pvalue)                            # 0.9983, 1.3e-13

Common mistakes

  • Reading r = 0 as “no relationship”. A U-shaped pattern, such as stress against performance, can have r close to zero while the relationship is strong. Always look at the scatter plot.
  • Mixing groups. Combining data from two cities or two product lines can create a correlation that exists in neither group on its own, or hide one that does.
  • Correlating totals that share a component. Revenue and profit, or total marks and one subject’s marks, correlate partly because one contains the other.

Related learning on Codeayan

FAQ

Correlation questions

What is a good correlation coefficient?

It depends on the field. A common rule of thumb reads |r| below 0.1 as negligible, 0.1 to 0.3 as weak, 0.3 to 0.5 as moderate, 0.5 to 0.7 as strong and 0.7 or above as very strong. In marketing or social data an r of 0.3 can be useful; in physics or engineering measurements, 0.9 may be disappointing.

When should I use Spearman instead of Pearson?

Use Spearman when the relationship is consistently up or consistently down but not a straight line, when the data has outliers, or when the values are ranks or ordinal ratings such as 1 to 5 scores. Use Pearson when you care about a straight-line relationship between two continuous variables. This calculator shows both so you can compare.

Does a significant correlation mean one variable causes the other?

No. A significant p-value only says the correlation is unlikely to be zero in the wider population. The link could run the other way, or both variables could be driven by a third factor such as season, city size or income.

How is r related to R² in regression?

For two variables, squaring Pearson's r gives the R² of a simple linear regression of one on the other. An r of 0.8 means the straight line explains 64% of the variation. Spearman's ρ does not have this interpretation.

How many data points do I need?

The calculator accepts 3 or more pairs and needs at least 4 for a confidence interval. With fewer than about 20 pairs the interval is wide, so even a large r may be consistent with a weak true relationship.

More tools

Keep going