Correlation Between Two Variables

In this method to calculate the correlation between two variables, the user has to simply call the corr() function from the base R, passed with the required parameters which will be the name of the variables whose correlation is needed to be calculated and further this will be returning the correlation detail between the given two variables in the R programming language.

Syntax:

cor(dataframe$column1, dataframe$column1)

where,

  • dataframe is the input dataframe
  • column1 is the column1 correlated with column2

Example:

Here, in this example, we are going to create the dataframe with 4 columns with 10 rows and find the correlation between col1 and col2,correlation between col1 and col3,correlation between col1 and col4 and correlation between col3 and col4 using the cor() function in the R programming language.

R




# create the dataframe with 4 columns
data=data.frame(col1=c(1:10),col2=c(11:20),
                col3=c(21:30),col4=c(1:10))
  
# correlation between col1 and col2
print(cor(data$col1,data$col2))
  
# correlation between col1 and col3
print(cor(data$col1,data$col3))
  
# correlation between col1 and col4
print(cor(data$col1,data$col4))
  
# correlation between col3 and col4
print(cor(data$col3,data$col4))


Output:

1
1
1
1

How to Calculate Correlation Between Multiple Variables in R?

In this article, we will discuss how to calculate Correlation between Multiple variables in R Programming Language. Correlation is used to get the relation between two or more variables:

  • The result is 0 if there is no correlation between two variables
  • The result is 1 if there is a positive correlation between two variables
  • The result is -1 if there is a negative  correlation between two variables

Let’s create an initial dataframe:

R




# create the dataframe with 4 columns
data=data.frame(col1=c(1:10),col2=c(11:20),
                col3=c(21:30),col4=c(1:10))
  
# display
data


Output:

   col1 col2 col3 col4
1     1   11   21    1
2     2   12   22    2
3     3   13   23    3
4     4   14   24    4
5     5   15   25    5
6     6   16   26    6
7     7   17   27    7
8     8   18   28    8
9     9   19   29    9
10   10   20   30   10

Similar Reads

Method 1: Correlation Between Two Variables

...

Method 2: Correlation Between Multiple Variables

In this method to calculate the correlation between two variables, the user has to simply call the corr() function from the base R, passed with the required parameters which will be the name of the variables whose correlation is needed to be calculated and further this will be returning the correlation detail between the given two variables in the R programming language....

Method 3: Correlation between all variables

...