How to find Covariance between two variables using R?

Covariance is the simplest and widely used measure of correlation. We can find the covariance between two variables in R using the cov function. Covariance measures the linear relationship between two variables in a dataset. A positive covariance value indicates a positive linear relationship between the variables, and a negative value represents the negative linear relationship. In a previous post, I have explained calculating covariance in a spreadsheet.

Steps to calculate Covariance in R

1. To illustrate how to calculate covariance in R. I use in-built women data. This data consists of two variables i.e. Average Heights and Weights of American Women. Load the inbuilt data using the following command

> data("women")

2.  Let’s find the covariance between the heights and weights in the dataset

> cov(women$height,women$weight)
[1] 69

The covariance result is 69. The result is a positive number, which denotes a positive relationship between the two variables. Remember the order you use in cov command doesn’t matter cov(women$height,women$weight) and cov(women$weight,women$height) both these will give the same result.

Read also: How to calculate descriptive statistics using R

One thought on “How to find Covariance between two variables using R?

  • February 19, 2019 at 16:42
    Permalink

    Very interesting subject , appreciate it for putting up.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.