Get Started

Finding overlapping indices

Time series are said to overlap when there are observations in both time series with the same index, meaning they occur at the same point in time.

By creating a subset using the %in% operator, the overlapping points can be filtered out of one of the time series, allowing the two datasets to be combined.

In this exercise, you'll take two time series, coffee and coffee_overlap, and remove the elements that overlap.

The datasets coffee and coffee_overlap, as well as the zoo and lubridate packages, are available to you already.

This is a part of the course

“Manipulating Time Series Data in R”

View Course

Exercise instructions

  • Use the value matching operator %in% to determine the indices of coffee_overlap that overlap with the indices of coffee.

  • Use square brackets ([, ]) and the negation operator (!) to extract the values of coffee_overlap which are not part of overlapping_index.

  • Combine the coffee time series with coffee_subset and view the resulting autoplot.

Hands-on interactive exercise

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

# Determine the overlapping indexes
overlapping_index <-
  ___ %in% ___

# Create a subset of the elements which do not overlap
coffee_subset <- ___[___]

# Combine the coffee time series and the new subset
coffee_combined <- ___

autoplot(coffee_combined)
Edit and Run Code

This exercise is part of the course

Manipulating Time Series Data in R

IntermediateSkill Level
4.8+
4 reviews

Master time series data manipulation in R, including importing, summarizing and subsetting, with zoo, lubridate and xts.

Here, you’ll learn to retrieve key attributes of time series information, such as the range in time of the data and how often the data were sampled, to understand your data better. You'll also be introduced to the zoo package, which contains tools and functions for creating and manipulating time series objects. Many data science applications in R use the data frame paradigm; you'll learn how to convert between a data frame and a time series.

Exercise 1: Time series attributesExercise 2: Temporal attributes and decimal datesExercise 3: Comparing temporal attributesExercise 4: Time series objects with the zoo packageExercise 5: Creating a zooExercise 6: zoo and ggplot2Exercise 7: Manipulating zoo objectsExercise 8: Updating and replacing indicesExercise 9: Finding overlapping indices
Exercise 10: Converting between zoo and data frameExercise 11: Moving between data.frame and zoo

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