The cor() function is used to calculate correlation between two variables in R. This function returns value between -1 to 1.
The following method shows how you can do it with syntax.
Method: Use cor() Function
cor(x,y)
The following example shows how to use cor() function in R.
Use cor() to Calculate Correlation Between Two Vectors
Let’s see how we can calculate correlation between two vectors using cor() function:
# Create vector
x <- c(77,85,89,85,82,88,81,89)
y <- c(77,78,79,81,84,85,85,89)
# Find correlation coefficient
cor(x,y)
Output:
[1] 0.3849002
As we can see in output the correlation coefficient between x and y vector is 0.3849002.
The cor() function return values between -1 to 1.When it return -1 means there negative correlation, 0 means no correlation and 1 mean positive correlation between variables.