Get Started

cor()relation

Did you notice the relationship between the two stocks? It seems that when Apple's stock moves up, Microsoft's does as well. One way to capture this kind of relationship is by finding the correlation between the two stocks. Correlation is a measure of association between two things, here, stock prices, and is represented by a number from -1 to 1. A 1 represents perfect positive correlation, a -1 represents perfect negative correlation, and 0 correlation means that the stocks move independently of each other. Correlation is a common metric in finance, and it is useful to know how to calculate it in R.

The cor() function will calculate the correlation between two vectors, or will create a correlation matrix when given a matrix.

cor(apple, micr)
[1] 0.9477011

cor(apple_micr_matrix)

          apple      micr
apple 1.0000000 0.9477011
micr  0.9477011 1.0000000

cor(apple, micr) simply returned the correlation between the two stocks. A large correlation of .9477 hints that Apple and Microsoft's stock prices move closely together. cor(apple_micr_matrix) returned a matrix that shows all of the possible pairwise correlations. The top left correlation of 1 is the correlation of Apple with itself, which makes sense!

This is a part of the course

“Introduction to R for Finance”

View Course

Exercise instructions

  • The vectors of stock prices for apple, micr, and ibm are in your workspace.
  • Calculate the correlation between apple and ibm.
  • Create a matrix of apple, micr, and ibm, in that order, named stocks using cbind().
  • Try to run the code for the correlation of all three stocks. Notice how it fails when using more than 2 vectors!
  • Rewrite the failing code to use the stocks matrix instead. Correlation matrices are very powerful when you have many stocks!

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Correlation of Apple and IBM


# stock matrix
stocks <- 

# cor() of all three
cor(apple, micr, ibm)

This exercise is part of the course

Introduction to R for Finance

BeginnerSkill Level
4.9+
11 reviews

Learn essential data structures such as lists and data frames and apply that knowledge directly to financial examples.

Chapter 1: The Basics

Get comfortable with the very basics of R and learn how to use it as a calculator. Also, create your first variables in R and explore some of the base data types such as numerics and characters.

Exercise 1: Welcome to Introduction to R for Finance!Exercise 2: Your first R scriptExercise 3: Arithmetic in R (1)Exercise 4: Arithmetic in R (2)Exercise 5: Assignment and variables (1)Exercise 6: Assignment and variables (2)Exercise 7: Financial returnsExercise 8: Financial returns (1)Exercise 9: Financial returns (2)Exercise 10: Basic data typesExercise 11: Data type explorationExercise 12: What's that data type?

Chapter 2: Vectors and Matrices

In this chapter, you will learn all about vectors and matrices using historical stock prices for companies like Apple and IBM. You will then be able to feel confident creating, naming, manipulating, and selecting from vectors and matrices.

Exercise 1: What is a vector?Exercise 2: c()ombineExercise 3: Coerce itExercise 4: Vector names()Exercise 5: Visualize your vectorExercise 6: Vector manipulationExercise 7: Weighted average (1)Exercise 8: Weighted average (2)Exercise 9: Weighted average (3)Exercise 10: Vector subsettingExercise 11: Matrix - a 2D vectorExercise 12: Create a matrix!Exercise 13: Matrix <- bind vectorsExercise 14: Visualize your matrixExercise 15: cor()relation
Exercise 16: Matrix subsetting

Chapter 3: Data Frames

Chapter 4: Factors

Questions with answers that fall into a limited number of categories can be classified as factors. In this chapter, you will use bond credit ratings to learn all about creating, ordering, and subsetting factors.

Exercise 1: What is a factor?Exercise 2: Create a factorExercise 3: Factor levelsExercise 4: Factor summaryExercise 5: Visualize your factorExercise 6: Bucketing a numeric variable into a factorExercise 7: Ordering and subsetting factorsExercise 8: Create an ordered factorExercise 9: Subsetting a factor

Chapter 5: Lists

Wouldn't it be nice if there was a way to hold related vectors, matrices, or data frames together in R? In this final chapter, you will explore lists and many of their interesting features by building a small portfolio of stocks.

Exercise 1: What is a list?Exercise 2: Create a listExercise 3: Named listsExercise 4: Access elements in a listExercise 5: Adding to a listExercise 6: Removing from a listExercise 7: A few list creating functionsExercise 8: Split itExercise 9: Split-Apply-CombineExercise 10: AttributesExercise 11: Congratulations!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free