Get Started

Handle missing values

In chapter 3, you used na.locf() to fill missing values with the previous non-missing value. You can use interpolation when carrying the previous value forward isn't appropriate. In this exercise, you will explore two interpolation methods: linear and spline.

Linear interpolation calculates values that lie on a line between two known data points. This is a good choice for fairly linear data, like a series with a strong trend. Spline interpolation is more appropriate for series without a strong trend, because it calculates a non-linear approximation using multiple data points.

Use these two methods to interpolate the three missing values for the 10-year Treasury rate in the object DGS10. Then compare the results with the output of na.locf().

This is a part of the course

“Importing and Managing Financial Data in R”

View Course

Exercise instructions

  • Complete the command to use na.approx() to fill in missing values using linear interpolation.
  • Complete the command to use na.spline() to fill in missing values using spline interpolation.
  • Merge locf, approx, and spline into one object named na_filled.
  • Complete the command to plot na_filled.

Hands-on interactive exercise

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

# fill NA using last observation carried forward
locf <- na.locf(DGS10)

# fill NA using linear interpolation
approx <- ___(DGS10)

# fill NA using spline interpolation
spline <- ___(DGS10)

# merge into one object


# plot combined object
___(___, col = c("black", "red", "green"))

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 values
Exercise 8: Visualize imported dataExercise 9: Cross reference sourcesExercise 10: Adjusting for corporate actionsExercise 11: Adjust for stock splits and dividendsExercise 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