← Back to R Course | Chapter 9: Statistics | Lesson 4 of 7

cor()

cor measures how strongly two sets of numbers move together, like whether taller kids tend to have bigger shoes. The answer runs from minus one to one.

In this page:

  1. cor()
Syntax
r
cor(x, y)

cor()

cor(x, y) computes the Pearson correlation coefficient between two numeric vectors, a value between -1 and 1 describing how strongly they move together linearly.

A value near 1 means strong positive correlation, near -1 means strong negative correlation, and near 0 means little linear relationship. It's a foundational tool for exploring relationships between variables.

Note: A correlation near 0 does not mean there is no relationship -- it only means there is no linear relationship.

Example: cor()

markup
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)

cat("Correlation:", cor(x, y), "\n")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Reading cor(x, y) as proof that x causes y, when correlation does not show cause.
  2. Passing vectors of different lengths, which gives an error.
  3. Using cor() on data with NA without use = "complete.obs", which returns NA.
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.