Correlation is a measure of linear strength and direction for two quantitative variables. The easiest way to calculate correlation is to use the cor()
function. Scatterplots and linear regression Lines. are often used to visualize the relationship between the two quantitative variables in question. The data for these examples comes from the cars
dataset.
cor(mtcars$drat, mtcars$qsec)
## [1] 0.09120476
The cor()
function returns NA
when there are missing values in the dataset. There are several ways to calculate correlation for missing data. The use = complete.obs
argument will ignore all cases with missing values.
cor(mtcars$drat, mtcars$qsec, use = "complete.obs")
## [1] 0.09120476
Mathematicss, Computer Science, and Statistics Department Gustavus Adolphus College