Reader outcome
This guide teaches a reproducible R workflow for calculating Pearson correlation, testing an association, handling missing values deliberately, reading a confidence interval and producing a thesis-ready result. It assumes that the reader has already decided that Pearson correlation is appropriate for the research question.
Version and scope
The commands are based on the base R stats documentation snapshot verified on 27 July 2026. cor() computes correlations or correlation matrices. cor.test() performs a test for association between paired samples and, for Pearson correlation, can return a confidence interval. Because R evolves, compare the commands and output with the documentation for the installed version.
Prepare a transparent example
Use a small synthetic dataset so the procedure can be reproduced without personal or confidential data.
study <- data.frame(
study_hours = c(4, 6, 5, 8, 9, 7, 10, 3, 6, 8, 11, 5),
exam_score = c(55, 61, 58, 72, 76, 68, 82, 50, 64, 74, 86, 59)
)
str(study)
summary(study)
complete.cases(study$study_hours, study$exam_score)
Check that both variables are numeric and that every row represents a correctly paired observation. If the imported data contain labels, character values or special missing codes, clean them explicitly rather than relying on automatic conversion.
Inspect the relationship before calculation
plot(
study$study_hours,
study$exam_score,
xlab = "Weekly independent-study hours",
ylab = "Final examination score",
pch = 19
)
abline(lm(exam_score ~ study_hours, data = study))
The plot should guide the choice of method. Look for a roughly linear pattern, influential observations, clusters and restricted ranges. The line is a visual aid, not evidence that the model assumptions are satisfied.
Compute a coefficient with cor()
cor(
study$study_hours,
study$exam_score,
method = "pearson",
use = "complete.obs"
)
method = "pearson" is the default, but writing it explicitly makes the analysis easier to audit. The use argument controls missing-value behaviour. Important options include:
everything: missing values propagate according to the default rules.complete.obs: use rows that are complete across the variables supplied; return an error when there are no complete cases.na.or.complete: similar complete-case behaviour, but can returnNAwhen there are no complete pairs.pairwise.complete.obs: calculate each pair using all rows complete for that pair.
Pairwise deletion can give different sample sizes across a correlation matrix. The official documentation warns that the resulting matrix can fail to be positive semidefinite and may contain NA values. Therefore, report the rule and inspect the effective sample size rather than choosing pairwise.complete.obs merely because it produces more numbers.
Test the association with cor.test()
pearson_test <- cor.test(
study$study_hours,
study$exam_score,
method = "pearson",
alternative = "two.sided",
conf.level = 0.95
)
pearson_test
unname(pearson_test$estimate)
pearson_test$conf.int
pearson_test$p.value
pearson_test$parameter
For Pearson correlation, the documented test statistic is based on a t distribution with n - 2 degrees of freedom under the stated null model. The confidence interval uses Fisher’s transformation when there are enough complete paired observations. A test result is not a replacement for inspecting the plot or assessing the study design.
Coefficient calculation versus hypothesis testing
cor() answers a descriptive question: what is the correlation in these data under the selected missing-value rule? cor.test() adds an inferential framework: under a sampling model, how compatible is the observed coefficient with a specified null value, and what range of population correlations is consistent with the data at the selected confidence level?
Do not treat these as interchangeable. A matrix generated by cor() is useful for exploration, but a thesis needs a declared inferential plan when p-values or confidence intervals are reported.
Create a reproducible result object
result <- data.frame(
variable_x = "Weekly independent-study hours",
variable_y = "Final examination score",
n_complete = sum(complete.cases(study$study_hours, study$exam_score)),
r = unname(pearson_test$estimate),
df = unname(pearson_test$parameter),
ci_low = pearson_test$conf.int[1],
ci_high = pearson_test$conf.int[2],
p_value = pearson_test$p.value
)
result
Round only for presentation. Keep full precision in the saved analysis object or output file. Record sessionInfo() so that the R version and platform can be reconstructed.
sessionInfo()
Correlation matrices
For several numeric variables:
numeric_data <- study[c("study_hours", "exam_score")]
cor(numeric_data, use = "complete.obs", method = "pearson")
A large exploratory matrix can create a multiple-testing problem if every pair is tested and selectively discussed. Define primary relationships in advance, distinguish confirmatory from exploratory analysis, and consider adjusted inference when many hypotheses are evaluated.
Thesis interpretation and reporting
A reporting template is:
The relationship was inspected using a scatterplot and showed an approximately linear positive pattern without an obviously dominant observation. A two-sided Pearson correlation based on 12 complete pairs indicated a positive association between weekly independent-study hours and examination score, r(10) = [coefficient], 95% CI [lower, upper], p = [value]. The analysis was conducted in R [version] using
stats::cor.test()with complete-case handling.
Insert actual values from the output. Do not hard-code values from this synthetic example into a real thesis.
Common errors
- Supplying factor or character variables without validating the conversion.
- Allowing missing-value handling to vary invisibly between analyses.
- Using pairwise deletion without reporting pair-specific sample sizes.
- Copying console output without saving code, data-processing decisions or version information.
- Reporting the p-value but omitting r, the confidence interval and the number of complete pairs.
- Interpreting a coefficient as causal, or as evidence that a linear predictive model is accurate.
Source and verification notes
The workflow is based on the R Core stats documentation for cor() and cor.test(). The code, example data, explanations and reporting template are original PT Writers instructional material. Record the installed R version and verify output semantics before using the workflow in assessed work.
Sources and verification
Links are preserved so readers can inspect the controlling documentation or underlying research.
- R: Correlation, Variance and Covariance (Matrices)R Core Team documentation mirrorAccessed 27 July 2026
- R: Test for Association/Correlation Between Paired SamplesR Core Team documentation mirrorAccessed 27 July 2026
Authorship and review
- Author
- PT Writers Research and Editorial Team
- Reviewer
- PT Writers Editorial Review
- Review scope
- Factual and editorial review
- Current status
- Source verified
Copy a formatted citation
Select the required referencing style, review the generated citation and copy it without leaving the guide.
PT Writers Research and Editorial Team. (2026). Compute, Test and Report Pearson Correlation in R. PT Writers. https://ptwriters.org/blog/pearson-correlation-in-r/