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:
Syntax
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()
x <- c(1, 2, 3, 4, 5)
y <- c(2, 4, 6, 8, 10)
cat("Correlation:", cor(x, y), "\n")
Login to try C/C++/Java/PHP code in the editor
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
- Reading
cor(x, y)as proof thatxcausesy, when correlation does not show cause. - Passing vectors of different lengths, which gives an error.
- Using
cor()on data withNAwithoutuse = "complete.obs", which returnsNA.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: