Written and maintained by CASRAI Editorial Board
Published
On this page
cor in R means the cor() function in the base stats package, which computes a correlation coefficient (Pearson, Kendall or Spearman) between two variables or among all columns of a matrix or data frame. This page is for researchers who need to run it, choose the right use= and method= arguments, and know when to switch to cor.test() to get a p-value and confidence interval. Documentation read 2026-11-08 (R-devel, stats 4.6.0).
What cor() is used for in research
The cor() function returns a coefficient only: one number for two vectors, or a correlation matrix when given a matrix or data frame. It does not return a p-value, a confidence interval or a significance statement. For those, use cor.test() (see below). For background on which coefficient suits which data, see types of correlation and Kendall tau correlation coefficient.
Install or access
cor(), cov(), var() and cor.test() belong to the stats package, which is part of R itself, so no extra package needs installing. R is distributed from the Comprehensive R Archive Network (cran.r-project.org). New to the language? Start with what R is and how it is used.
The cor function in R: syntax
cor(x, y = NULL, use = "everything", method = c("pearson", "kendall", "spearman"))
x: a numeric vector, matrix or data frame.y:NULL(the default, which is the same asy = x) or a vector, matrix or data frame with dimensions compatible withx.use: how missing values are handled (table below).method:"pearson"(the default, the first listed),"kendall"or"spearman".
Source: R documentation for cor, stat.ethz.ch/R-manual/R-devel/library/stats/html/cor.html, read 2026-11-08.
Step by step: correlation in R
The examples use mtcars, a data set that ships with R, so they run without loading any file.
- Correlate two variables.
cor(mtcars$mpg, mtcars$wt)This returns a single coefficient (Pearson, because no
methodwas given). - Choose a rank-based method.
cor(mtcars$mpg, mtcars$wt, method = "spearman")cor(mtcars$mpg, mtcars$wt, method = "kendall")Spearman is effectively
cor(R(x), R(y))whereRis the rank function. Kendall is rank-based and the documentation describes it as more robust when the data may not be bivariate normal; with ties, tau-b is computed. - Build a correlation matrix.
cor(mtcars[, c("mpg", "wt", "hp", "disp")])With a matrix or data frame as
xandyleft asNULL, every column is correlated with every other column, giving a square matrix. - Handle missing values with
use=.cor(df$a, df$b, use = "complete.obs")cor(df, use = "pairwise.complete.obs", method = "spearman")Here
dfstands for your own data frame. See the table below before choosing. - Get a p-value and confidence interval.
res <- cor.test(mtcars$mpg, mtcars$wt)res$estimateres$p.valueres$conf.int
The five use= options
| use = | Documented behaviour |
|---|---|
"everything" (default) |
Missing values propagate: any result that has an NA contributor is NA. |
"all.obs" |
Any missing value produces an error. |
"complete.obs" |
Rows with any missing value are removed first; an error if there are no complete cases. |
"na.or.complete" |
Same as "complete.obs", but returns NA instead of an error when there are no complete cases. |
"pairwise.complete.obs" |
Each pair of variables uses its own complete pairs. The resulting matrix may not be positive semi-definite and may contain NA entries. For cov and var it works only with Pearson. |
Source: R documentation for cor, read 2026-11-08. A covariance matrix has the same shape; see covariance matrix.
Output and how to read it
A coefficient from cor() lies between -1 and 1; the documentation states that all(abs(r) <= 1) is guaranteed when use = "all.obs". A positive value means the variables tend to rise together, a negative value that one tends to fall as the other rises. The function returns NA when there is only one observation.
cor() versus cor.test()
| cor() | cor.test() | |
|---|---|---|
| Returns | Coefficient or matrix | An object of class htest |
| Contents | Numbers only | statistic, parameter (degrees of freedom), p.value, estimate, null.value (0), alternative, method, data.name, conf.int |
| Name of the estimate | n/a | cor (Pearson), tau (Kendall), rho (Spearman) |
| Confidence interval | No | Pearson only, needs at least 4 complete pairs, asymptotic (Fisher Z). Kendall and Spearman return no interval. |
| Alternative hypothesis | n/a | alternative = "two.sided" (default), "less" (negative association) or "greater" (positive association) |
cor.test() also accepts a formula, for example cor.test(~ mpg + wt, data = mtcars), and uses conf.level = 0.95 by default. For Kendall and Spearman, exact controls exact p-values (NULL lets R choose). Source: R documentation for cor.test, read 2026-11-08.
A coefficient describes association, not cause. Report it with the method used and how missing data were handled.
Troubleshooting
- The result is NA. With the default
use = "everything", an NA in a column makes the correlations involving that column NA. Choose"complete.obs"or"pairwise.complete.obs"deliberately. - An error when data contain NA. This is the documented behaviour of
use = "all.obs". - An error with no complete cases.
"complete.obs"raises an error if there are no complete cases;"na.or.complete"returns NA instead. - Matrix has NA entries after pairwise deletion. The documentation warns this can happen with
"pairwise.complete.obs", and the matrix may not be positive semi-definite. - Kendall is slow on large samples. The documentation notes it is slow beyond roughly 1000 cases and points to
cor.fkin the pcaPP package. - No confidence interval from
cor.test(). Intervals exist only for the Pearson method and need at least 4 complete pairs.
Free alternatives
Excel is the common non-R route and is covered in how to find the correlation coefficient in Excel; it is not free software. Within R, once you have a correlation matrix, VIF in R covers checking collinearity among predictors.
FAQ
What does cor() do in R?
It computes a Pearson, Kendall or Spearman correlation coefficient between vectors, or a matrix of coefficients between the columns of a matrix or data frame.
What is the default method of the cor function in R?
Pearson. The documented method choices are "pearson", "kendall" and "spearman", with Pearson listed first.
How do I get a p-value for a correlation in R?
Use cor.test(x, y) and read $p.value; cor() does not compute one.
How do I run cor() when there are missing values?
Set use. The default propagates NA; "complete.obs" drops incomplete rows; "pairwise.complete.obs" uses each pair’s own complete cases.
Sources
- R Core Team. Correlation, Variance and Covariance (Matrices): cor, R-devel stats 4.6.0 manual. Read 2026-11-08.
- R Core Team. Test for Association/Correlation Between Paired Samples: cor.test, R-devel stats 4.6.0 manual. Read 2026-11-08.
- The Comprehensive R Archive Network, cran.r-project.org (official download source for R).







