Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Correlation


Share

Correlation

In the Scatter Plot chapter, the plot of 12 cars seemed to show that the newer the car, the faster it drives.

Correlation turns that kind of relationship into one number, between -1 and 1:

Value Meaning
1Perfect positive relationship: when one value goes up, the other goes up too
0No relationship
-1Perfect negative relationship: when one value goes up, the other goes down

The closer the value is to 1 or -1, the stronger the relationship.


The cor() Function

Do heavier cars drive fewer miles per gallon? First, look at the weight (wt) and the miles per gallon (mpg) of the cars in the mtcars data set in a scatter plot:

Example

Data_Cars <- mtcars

plot(Data_Cars$wt, Data_Cars$mpg, main = "Weight and Miles per Gallon", xlab = "Weight", ylab = "Miles per gallon")

Result:

Scatter plot where heavier cars drive fewer miles per gallon
Try it Yourself »

The dots go down from left to right. Now use the cor() function to measure the relationship:

Example

Data_Cars <- mtcars

cor(Data_Cars$wt, Data_Cars$mpg)

Result:

[1] -0.8676594
Try it Yourself »

The result is close to -1, which is a strong negative relationship: the heavier the car, the fewer miles it drives per gallon.



Positive Correlation

The weight and the horsepower (hp) have a positive relationship: heavier cars often have stronger engines:

Example

Data_Cars <- mtcars

cor(Data_Cars$wt, Data_Cars$hp)

Result:

[1] 0.6587479
Try it Yourself »

Note: A correlation does not prove that one thing causes the other. It only shows that the two values often change together.


Covariance

The cov() function finds the covariance. It also shows if two values go up and down together (positive) or in opposite directions (negative):

Example

Data_Cars <- mtcars

cov(Data_Cars$wt, Data_Cars$mpg)

Result:

[1] -5.116685
Try it Yourself »

But the size of the covariance depends on the units of the data, so it is hard to say if a number is big or small. That is why correlation is used more often: it is the covariance scaled to a number between -1 and 1.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.