# How to Calculate Covariance and Correlation in R Programming

In this article, we will learn about how to calculate two important statistics – covariance and correlation in R programming.

Both covariance and correlation are indicators of relationship between two variables, in terms of how they move with respect tom each other. If they move in the same direction, there is a positive relationship, but if they move in the opposite direction (when one goes up, the other goes down), then there is a negative relationship. Learn more about covariance and correlation and their formulas.

While covariance indicates the direction of relationship between two variables, correlation indicates both the direction and the strength of the relationship. Correlation is actually a translation of covariance into a unit-less standardized measure that we can understand (-1.0 to 1.0).

Let’s take two vectors containing stock prices of two stocks over the past 10 days.

```
AAPL <- c(92.04, 93.59, 94.4, 95.6, 95.89, 94.99, 95.53, 95.94, 96.68, 96.98, 97.42, 96.87, 98.79, 98.78, 99.83, 99.87, 99.96, 99.43, 98.66, 97.34)
MSFT <- c(48.43, 49.44, 50.54, 51.17, 51.16, 51.17, 51.38, 51.38, 52.3, 52.59, 53.21, 53.51, 53.74, 53.7, 53.96, 53.09, 55.91, 55.8, 56.57, 56.73)
```

### Covariance in R Programming

In R programming, covariance can be calculated using the cov() function. This function takes three parameters: the two vectors and a method to calculate the covariance. The default method is Pearson. The three methods available are "pearson", "kendall", and "spearman".

Applying this formula to our two stocks:

```
result_cov <- cov(AAPL, MSFT, method="spearman")
result_cov
[1] 29.92105
```

We have a positive covariance of 29.92.

### Correlation in R Programming

We can calculate correlation of these two vectors using the cor() function in R programming. It takes the same three parameters as the cov() function.

```
result_cor <- cor(AAPL, MSFT, method="spearman")
result_cor
[1] 0.8555307
```

We have a positive correlation of 0.85 which indicates a very strong relationship between the two stocks.

### Converting Covariance to Correlation

R also provides a function to convert covariance to correlation. This function cov2cor() function takes only one input. This method, however, doesn’t work with vectors. It converts the covariance matrix into a correlation matrix of values. The matrix must be a square matrix. In most cases you won’t need to use it as you can directly calculate both the statistics from your source data.

### You may find these interesting

## Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.