Finance Train LogoFinance Train
Learning LibraryTemplatesBlog
Data Science Bundle
Finance TrainFinance Train
Learning LibraryTemplatesBlog
Data Science Bundle
📊R Programming

How to Calculate Covariance and Correlation in R Programming

October 19, 2022
2 min read

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.

1    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)
2    
3    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)
4

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:

1    result_cov <- cov(AAPL, MSFT, method="spearman")
2    
3    result_cov
4    
5    [1] 29.92105
6

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.

1    result_cor <- cor(AAPL, MSFT, method="spearman")
2    
3    result_cor
4    
5    [1] 0.8555307
6

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.

Learning Library

Practical resources on data science, machine learning, and AI for finance professionals.

Browse Library
Mini Roadmap - Data Science for Finance
Free Download

Mini Roadmap: Data Science for Finance

A step-by-step guide covering Python, SQL, analytics, and finance applications.

Finance Train

Learn data science and AI skills for finance through practical courses and tutorials.

Learn

  • Learning Library
  • Course Directory
  • Blog

Resources

  • Templates & Downloads
  • Tools
  • Tables
  • Calculators

Company

  • About
  • Contact
  • Privacy
  • Terms

© 2026 Finance Train. All rights reserved.