Skip to main content
v2026.11,986 entries · CC-BY 4.0

cor in R: cor(), use= and method= with Examples

cor in R from the official docs: cor() syntax, use= options for NA handling, method choices, and cor.test() for p-values.

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 as y = x) or a vector, matrix or data frame with dimensions compatible with x.
  • 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.

  1. Correlate two variables.
    cor(mtcars$mpg, mtcars$wt)

    This returns a single coefficient (Pearson, because no method was given).

  2. 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)) where R is 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.

  3. Build a correlation matrix.
    cor(mtcars[, c("mpg", "wt", "hp", "disp")])

    With a matrix or data frame as x and y left as NULL, every column is correlated with every other column, giving a square matrix.

  4. Handle missing values with use=.
    cor(df$a, df$b, use = "complete.obs")cor(df, use = "pairwise.complete.obs", method = "spearman")

    Here df stands for your own data frame. See the table below before choosing.

  5. 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.fk in 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

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about cor in R: cor(), use= and method= with Examples

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →