The sample correlation coefficient, written r, is a single number that sums up how closely two sets of paired measurements follow a straight line. Computing it comes down to one idea: checking whether points that are above average on one measure tend also to be above average on the other. The idea goes back to Francis Galton’s study of inherited traits in sweet peas, which Karl Pearson turned into the r still used today.1Journal of Statistics Education. Galton, Pearson, and the Peas: A Brief History of Linear Regression for Statistics Instructors Several formulas you’ll see in textbooks look different yet always give the same answer, so you can pick the one that suits your tools.
Step-by-Step Calculation of r by Hand
The standard formula divides the sum of (x minus its average) times (y minus its average) by the square root of the summed squared x-distances times the square root of the summed squared y-distances.2Wikipedia. Pearson correlation coefficient Every piece of it is built from each value’s distance from its own average (the mean), which statisticians call its deviation.
The steps, in order:
- Step 1: Find the average of all the x values and the average of all the y values.
- Step 2: Subtract the x average from each x, and the y average from each y.
- Step 3: Multiply each pair’s two deviations together and add up the products.
- Step 4: Square each x deviation and add them up. Do the same for y.
- Step 5: Multiply those two sums, take the square root, and divide the Step 3 total by it.
When both values in a pair sit above their averages, or both sit below, the product is positive. Mostly matching pairs push r toward a positive value, while mismatched pairs push it negative. The top of the fraction measures how much the two variables move together. The bottom rescales that by how much each variable moves on its own, which keeps the answer between −1 and 1 and independent of the units.
Here is a worked example with five students: hours studied (x) and quiz score (y). The x values are 1, 2, 3, 4, 5 and the y values are 52, 60, 58, 70, 75. The x average is 3 and the y average is 63. Each row gets its two deviations, their product, and both squares. For the first student, the x deviation is 1 − 3 = −2, the y deviation is 52 − 63 = −11, the product is 22, and the squares are 4 and 121. Filling in the other four rows gives three totals: the products add to 56, the squared x deviations to 10, and the squared y deviations to 348. The final division is 56 divided by the square root of 10 × 348, which is about 59, so r is about 0.95.
A few slips cause most wrong answers by hand. People subtract one overall average from every value instead of using each variable’s own average. They drop the minus signs on deviations before multiplying. They take the square root of only one of the two sums, or forget to multiply the two sums together before taking the root. And they round the averages too early, which shifts the final r.
You can check your work in three ways. The deviations for each variable should add to zero, apart from rounding. Your final r must land between −1 and 1, so anything outside that range means an error. And you can compare with a spreadsheet’s CORREL function on the same data.
The bottom can also collapse. If every x value is identical, every x distance from the average is zero, so the bottom of the fraction becomes zero. Dividing by zero means r cannot be computed, and spreadsheets will show an error such as #DIV/0!. That is not a calculation slip: with no variation in x, there is no pattern to measure. The same holds if every y value is identical, so check that both columns actually vary before you start.
Other Formulas That Give the Same Answer
Rearranged, r equals n times the sum of xy minus the sum of x times the sum of y, divided by the square root of n·Σx² − (Σx)² times the square root of n·Σy² − (Σy)².2Wikipedia. Pearson correlation coefficient This version needs only five running totals (Σx, Σy, Σxy, Σx², Σy²), with no averages computed first.
Run the five-student example through it. The totals are Σx = 15, Σy = 315, Σxy = 1001, Σx² = 55, and Σy² = 20193. The top is 5 × 1001 − 15 × 315 = 280. The two pieces under the roots are 5 × 55 − 15² = 50 and 5 × 20193 − 315² = 1740. The root of 50 × 1740 is about 295, and 280 divided by 295 gives 0.95 again, so the formulas agree.
A third version uses z-scores. A z-score (also called a standard score) says how many typical spreads, known as standard deviations, a value sits from its average.2Wikipedia. Pearson correlation coefficient Then r equals the average of the products of each pair’s two z-scores.3PEP 6305 Measurement in Health & Physical Education. Topic 6: Correlation Another way to write it is the covariance of x and y divided by both standard deviations.4Scribbr. Correlation Coefficient | Types, Formulas & Examples
There is a catch with the z-score route. Sample standard deviations divide by n − 1, so the z-score products are also averaged by dividing by n − 1 to match. Mixing n and n − 1 gives a slightly wrong r.
Use the deviation formula when you’re learning, because each step has a visible meaning. Use the raw-sums shortcut on a basic calculator with summation keys. A course may ask for one specifically, so check what your instructor expects.
The shortcut is not always the easiest by hand. It avoids subtracting averages from every row, which saves steps when the averages are awkward decimals. But it produces large totals, so the subtractions in the bottom of the formula involve big, nearly equal numbers. With a few small whole-number pairs it is quick and clean, whereas with large or long data it is easier to slip. Both routes give the same r, so choosing is about convenience.
Covariance alone is hard to interpret because it carries units of x times units of y, such as hours times points, so its size depends on how you measure. A covariance of 40 could be a strong or weak relationship depending on how spread out the variables are. Dividing by both standard deviations cancels the units and gives the unit-free r.
What Your Value of r Tells You
r always lies between −1 and 1.2Wikipedia. Pearson correlation coefficient A value of 1 means every point sits on a rising line, −1 means every point sits on a falling line, and 0 means no straight-line pattern.
r doesn’t change if you shift or rescale either variable by a positive amount.2Wikipedia. Pearson correlation coefficient Measuring height in inches or centimetres, or temperature in Celsius or Fahrenheit, gives the same r. The one exception is multiplying a variable by a negative number, which flips the sign of r but not its size. Reverse-scored survey items are a common case.
Students often use rough labels for size. In many fields, values near 0.1 are called weak, near 0.3 moderate, and 0.5 or more strong. These labels vary by field, so check your course’s convention. Strength here means how tightly the points hug a line, not how steep the line is.
At r near 0.9 the points form a narrow band hugging a line, and you could predict y from x fairly well. At r near 0.5 you see a visible slant but a wide, loose cloud around it. At r near 0 the cloud has no tilt, and at negative values the same pictures tilt downward.
Squaring r gives the share of the variation in y linked to the line. An r of 0.5 gives 0.25, so only a quarter, which is smaller than it sounds.
An r near 0 means there is no straight-line trend, not that the variables are unrelated. A curved pattern, such as a U-shape, can produce an r close to 0 even though y depends strongly on x. Only a scatterplot can tell you whether the points form a shapeless cloud or a clear curve. A high r says nothing about which variable drives which, or whether either does.
Computing r in Excel, Calculators, and Code
Excel and Google Sheets use =CORREL(range1, range2). On a TI-84, r appears after LinReg once diagnostics are turned on. Python’s numpy.corrcoef and R’s cor() return the same value.
Computers can still get it wrong. The popular textbook shortcut can lose accuracy when values are large and close together.5Proceedings of the 30th International Conference on Scientific and Statistical Database Management. Numerically stable parallel computation of (co-)variance It subtracts two huge, nearly equal totals, so tiny rounding errors swamp the true difference. Updating the averages step by step as each data point arrives avoids this and is only about 20% slower.6Czech Technical University in Prague. Optimization of Pearson correlation coefficient calculation for DPA and comparison of different approaches
You can use the same idea by hand. Subtract a round number from all the x values first, such as 1000 from a column of years. Because shifting doesn’t change r, the answer stays correct but the arithmetic is safer.
Missing data adds another wrinkle. If some rows are missing values, software either drops any incomplete row or uses every available pair for each correlation.7CASRAI. Listwise vs. Pairwise Deletion for Missing Data Using different rows for different pairs can produce a set of correlations that is mathematically impossible with complete data.8jamovi. Exclude cases pairwise in linear regression
Check that the software used the right columns. CORREL needs two ranges of the same length with values in matching rows, or it returns an error. Excel ignores rows where either cell holds text or is blank, so check that gaps in your columns are really missing data and not entry errors. Sorting one column without the other destroys the pairs, so sort the entire table together.
If your hand answer differs from the software’s in the third or fourth decimal place, the usual cause is early rounding of averages. A difference in the first decimal or a different sign suggests a mistake such as swapped values or a wrong step. Some tools report r squared instead of r, so a value that looks too small might be the squared version.
Testing Whether r Is Real or Chance
The test statistic is t = r times the square root of (n − 2), divided by the square root of (1 − r²).9Lumen Learning. How to Compute the Sample Correlation Coefficient Compare it to a t-distribution with n − 2 degrees of freedom, since two quantities (the line’s intercept and slope) are estimated from the data.10Introductory Statistics. How to Compute the Sample Correlation Coefficient “Degrees of freedom” is the number that tells the distribution how much independent information your data holds. Calculators and software print the matching p-value alongside t.9Lumen Learning. How to Compute the Sample Correlation Coefficient
For the five-pair example, r is about 0.95 and n is 5, so there are 3 degrees of freedom. The formula gives t of about 5.2, large enough to count as significant at the usual 0.05 cutoff. A moderate r tells a different story. With the same five pairs, an r of 0.5 gives t of only about 1, which is nowhere near significant.
If the p-value falls below your chosen cutoff, often 0.05, you call the correlation statistically significant. A larger absolute t, from a bigger r or a bigger n, gives a smaller p-value. Significant means the link is unlikely to be pure chance, not that it is strong or important.
Sample size changes the verdict. The same r of 0.5 is unremarkable with five pairs but convincing with a hundred. With very large samples, even a tiny r can be significant while being practically meaningless. So report r itself along with the p-value, since each answers a different question.
Small samples are unstable in another way. Sample correlations settle toward the true value as samples grow but can be far off in small samples.11Journal of Research in Personality. At what sample size do correlations stabilize? On top of that, r slightly underestimates the true correlation, by as much as.03 or.04 in realistic cases.12Psicológica. Bias in Estimation and Hypothesis Testing of Correlation That bias hardly matters when you only want to know whether a link exists. It is worth noting when an exact size of correlation is the goal.
Confidence ranges for r usually use the Fisher z-transformation.13Statistics & Probability Letters. Confidence intervals for the correlation coefficient It reshapes r so its uncertainty becomes symmetric. You build the range on the z scale and then convert back to r. Software usually does this for you.
When r Misleads and What to Use Instead
Famous example datasets show very different patterns, even a dinosaur drawing, while sharing nearly the same summary numbers.14PubMed Central. An efficient, not-only-linear correlation coefficient based on clustering Make a scatterplot before trusting r.
Extreme values can distort r a lot, especially when one point is extreme on both variables at once.15Finance Research Letters. The instability of the Pearson correlation coefficient in the presence of coincidental outliers First check whether the point is a typing or measurement error, and correct it if the true value can be found. If it is a real observation, don’t delete it just because it hurts the result. Report r with and without it, and explain what the point is. A rank-based measure like Spearman’s is less affected by a single extreme value.
Narrow ranges and noisy measures also shrink r.16Journal of Educational Statistics. Corrections for Attenuation and Range Restriction on the Predictor Admission test scores, for example, correlate weakly with grades among admitted students. Low scorers were never admitted, so the range is cut off, and the true link across all applicants is stronger. Formulas exist to correct r for range restriction, given the share of people selected.17British Journal of Mathematical and Statistical Psychology. The correction of correlation coefficients for restriction of range when restriction results from the truncation of a normally distributed variable
Measurement error adds scatter unrelated to the real pattern, which pulls r toward zero. A low r between two sloppy measures may therefore hide a stronger underlying relationship. Spearman’s correction estimates the correlation as if measurements were perfectly reliable,18PubMed Central. Modifying Spearman’s Attenuation Equation to Yield Partial Corrections for Measurement Error—With Application to Sample Size Calculations but it requires knowing how reliable each measure is, which is rarely available in a quick analysis. For most readers, the practical point is to treat a weak r from noisy data as a lower bound, not a final verdict.
Rank-based alternatives help when the pattern is not a straight line. Spearman’s correlation is simply Pearson’s r computed on the ranks of the data.19Wikipedia. Spearman’s rank correlation coefficient It measures whether y keeps rising or falling with x, even along a curve.19Wikipedia. Spearman’s rank correlation coefficient To compute it by hand, rank x from smallest to largest, then rank y the same way. Give tied values the average of the ranks they share, then run the steps from the first section on the ranks instead of the raw values. Kendall’s tau takes a different approach, counting pairs of points that move in the same direction versus opposite directions.20Wikipedia. Kendall rank correlation coefficient Spearman’s correlation, like Pearson’s r, can come out near zero for a U-shaped pattern, so the scatterplot remains essential.
For choosing among the three, use Pearson for straight-line numeric data without big outliers. Use Spearman for ranked data or steady but curved trends. Kendall’s tau is often preferred for small samples with many ties.

