Get Started

Adjust for stock splits and dividends

Stock splits can create large historical price changes even though they do not change the value of the company. So, you must adjust all pre-split prices in order to calculate historical returns correctly.

Similarly, you must adjust all pre-dividend prices. Dividends do reduce the company's value by the amount of the dividend payment, but the investor's return isn't affected because they receive the offsetting dividend payment.

In this exercise, you will learn how to use the adjustOHLC() function to adjust raw historical OHLC prices for splits and dividends, so historical returns can be calculated accurately.

Yahoo Finance provides raw prices and a split- and dividend-adjusted close column. The output of adjustOHLC() should match Yahoo's adjusted close column. AAPL data from Yahoo Finance is already loaded in your workspace.

While not necessary to complete this exercise, Yahoo Finance provides an accessible example of the adjusted close calculation, if you're interested in learning more.

This is a part of the course

“Importing and Managing Financial Data in R”

View Course

Exercise instructions

  • Look at the first few rows of AAPL
  • Use adjustOHLC() to adjust the AAPL data. Assign the result to an object named aapl_adjusted.
  • Look at the first few rows of aapl_adjusted. Notice the close column of aapl_adjusted is the same as the adjusted close column of AAPL.

Hands-on interactive exercise

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

# Look at first few rows of AAPL


# Adjust AAPL for splits and dividends


# Look at first few rows of aapl_adjusted
Edit and Run Code

This exercise is part of the course

Importing and Managing Financial Data in R

IntermediateSkill Level
3.7+
3 reviews

Learn how to access financial data from local files as well as from internet sources.

You've learned the core workflow of importing and manipulating financial data. Now you will see how to import data from text files of various formats. Then you will learn how to check data for weirdness and handle missing values. Finally, you will learn how to adjust stock prices for splits and dividends.

Exercise 1: Importing text filesExercise 2: Import well-formatted daily OHLC dataExercise 3: Import text files in other formatsExercise 4: Handle date and time in separate columnsExercise 5: Read text file containing multiple instrumentsExercise 6: Checking for weirdnessExercise 7: Handle missing valuesExercise 8: Visualize imported dataExercise 9: Cross reference sourcesExercise 10: Adjusting for corporate actionsExercise 11: Adjust for stock splits and dividends
Exercise 12: Download split and dividend dataExercise 13: Adjust univariate data for splits and dividendsExercise 14: When to adjust dataExercise 15: 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