
Correlation in R: Choosing and Interpreting a Method
Choose and interpret correlation in R using a paired teaching dataset. Compare Pearson and Spearman, handle missing values and report association without causal overclaims.
On this page
- 1. Define the paired observations
- 2. Enter and inspect a teaching dataset
- 3. Choose Pearson or Spearman for a reason
- 4. Handle missing values without breaking pairs
- 5. Calculate Pearson correlation and inspect its interval
- 6. Compare a rank-based analysis when justified
- 7. Examine outliers, subgroups and causal claims
- 8. Write a clear results paragraph
- Questions readers ask
- Official references and further reading
Correlation in R helps describe how two variables move together. A coefficient is useful only when it matches the question, the measurements and the pattern in the data. A large number can be misleading when it is driven by one unusual observation, combines unrelated groups or is presented as evidence of cause and effect.
This guide uses a small constructed dataset to compare Pearson and Spearman correlation, handle incomplete pairs explicitly and report a result with appropriate limits. The numbers are teaching values, not findings from a real student study.
1. Define the paired observations
Suppose you record study hours and an assessment score for each learner. Each row should contain both measurements for the same learner over a defined period. Pairing one learner’s hours with another learner’s score does not answer the intended question, even if the vectors happen to have the same length.
Decide which population and setting the question concerns. Measurements taken repeatedly from the same person are not independent observations. A pooled correlation that ignores repeated measurements or classroom clustering can give misleading uncertainty estimates.
Before calculating a coefficient, review our guide to data collection methods. Good analysis cannot recover information that was never measured appropriately.
2. Enter and inspect a teaching dataset
Use a standard R installation; no additional package is required for these examples. If you need setup help, start with installing R and RStudio.
d <- data.frame(
hours = c(1, 2, 3, 4, 5, 6, 7, 8),
score = c(48, 52, 51, 60, 58, 65, 67, 69)
)
str(d)
summary(d)
plot(score ~ hours, data = d, pch = 16,
col = "#1658b9",
xlab = "Study hours", ylab = "Assessment score")The scatterplot should show one point per row. Examine the overall shape, isolated points and any apparent grouping. A curved pattern can have a low linear correlation even when the variables are strongly related in another way.
Check measurement definitions as carefully as the plot. Are hours self-reported or recorded? Do scores come from the same assessment? Was the study period comparable for everyone? Those details affect interpretation without appearing in the coefficient itself.
3. Choose Pearson or Spearman for a reason
| Method | Describes | Important considerations |
|---|---|---|
| Pearson | Linear association between numeric measurements | Sensitive to unusual observations; conventional inference needs suitable sampling and distributional assumptions |
| Spearman | Monotonic association based on ranks | Useful for rank-oriented questions; ties affect calculation and inference |
Pearson correlation is not simply the “numeric data” choice and Spearman is not a universal repair for every problem. A rank coefficient answers a different question. If the relationship rises and then falls, neither a single Pearson nor a single Spearman value may adequately describe it.
R also provides Kendall’s rank correlation. The official cor.test documentation describes all three methods and the conditions affecting their tests and intervals.
4. Handle missing values without breaking pairs
Remove an incomplete pair together rather than dropping missing values separately from each vector. Separate deletion can shift rows and accidentally match measurements from different people. Keep a record of how many rows were excluded and investigate whether missingness could affect the conclusion.
keep <- complete.cases(d[c("hours", "score")])
sum(!keep) # Number of excluded rows
analysis_data <- d[keep, ]
nrow(analysis_data)For the complete teaching dataset, no rows are excluded. Complete-case analysis is transparent here, but it is not automatically appropriate for every real study. If missingness is substantial or related to the outcome, the analysis may need a more carefully justified approach.
5. Calculate Pearson correlation and inspect its interval
pearson_result <- cor.test(
analysis_data$hours,
analysis_data$score,
method = "pearson"
)
pearson_result
pearson_result$estimate
pearson_result$conf.intThe example Pearson coefficient is approximately 0.9686. It describes a strong positive linear pattern within these eight constructed observations. Its two-sided p-value is approximately 0.00007546 under the test assumptions.
Inspect the confidence interval rather than reporting only a threshold such as “p < 0.05.” A small p-value does not tell you how useful the relationship is for a real decision. With a small dataset, individual observations can have considerable influence even when a coefficient appears large.
A coefficient of 0.9686 does not mean that scores increased by 96.86 percent. Correlation has no original measurement unit and does not describe the number of score points associated with an extra study hour. A regression model addresses a different, more specific question about the outcome scale.
6. Compare a rank-based analysis when justified
spearman_result <- cor.test(
analysis_data$hours,
analysis_data$score,
method = "spearman",
exact = FALSE
)
spearman_resultThe Spearman coefficient for these values is approximately 0.9524. Here exact = FALSE explicitly requests the asymptotic calculation for the p-value. Use the official documentation to understand the default behavior for your data, particularly when ties occur.
Do not choose whichever method gives the smaller p-value. Explain the measurement scale and association you wanted to describe. Comparing methods can be a useful sensitivity check, but the comparison itself should be reported transparently.
7. Examine outliers, subgroups and causal claims
Imagine adding one learner with an exceptionally high score and recorded hours far beyond everyone else. That point may strongly affect Pearson correlation. Check whether it is a data-entry error, a valid unusual observation or evidence that the simple analysis is incomplete. Do not remove it solely to improve the result.
Also inspect meaningful subgroups. A combined dataset can show a pattern that differs from the patterns within its groups. If assessments or learning conditions differ across classes, a pooled coefficient may answer a different question from the one a teacher has in mind.
Most importantly, association alone does not establish that increasing study hours would cause the observed score change. Prior knowledge, study quality, assessment difficulty and other factors may matter. The teaching dataset cannot identify those explanations because it contains no real study design or participant information.
8. Write a clear results paragraph
An illustrative report is: “For eight constructed pairs of study hours and scores, Pearson correlation was r ≈ 0.9686. The scatterplot showed a positive pattern. These values demonstrate the calculation and cannot support claims about actual learners or a causal effect.”
In a real report, include the number of complete pairs, variable definitions, coefficient, interval where appropriate, method, treatment of missing values and relevant sampling limitations. Mention any sensitivity analysis and explain what decision, if any, the result informs.
If you want to model score differences in their original units, continue with linear regression in R. If the predictor is a group label, the one-way ANOVA guide may be a more useful next step.
- Confirm that each pair refers to the same observational unit.
- Plot the data and inspect unusual values and subgroups.
- Choose a coefficient that fits the relationship of interest.
- Document incomplete pairs and any exclusions.
- Report the coefficient with uncertainty and study context.
- Separate association from causal interpretation.
- Keep the script, source data documentation and session information.
Questions readers ask
Does correlation show that one variable causes another?
No. Causal claims require an appropriate study design and consideration of alternative explanations.
Should I always use Spearman when data look unusual?
No. Spearman describes rank-based monotonic association. It is not a universal fix for dependence, measurement problems or a non-monotonic relationship.
What is the difference between cor and cor.test in R?
cor calculates a correlation coefficient. cor.test performs an association test and returns additional inferential output, with details depending on the selected method.
Can I remove missing observations from each variable separately?
Not for paired analysis. Handle incomplete pairs together so measurements remain matched to the same observational unit.
Official references and further reading
Sources checked 19 October 2026. Follow the linked official documents for current software requirements and method details. The examples are educational and do not establish results from an actual study.
Editorial update: Oxford Training and Research. Prepared with AI-assisted drafting; the original author record is retained. Worked examples are illustrative. No independent field study or specialist review is claimed.

