Get startedGet started for free

Wrangling time series

In the last exercise, you created an xts object of gas prices. In this exercise, you will take a closer look at the year 2014.

You can index a time series object by a vector of dates. The following code

dates <- c(as.Date("2014-06-30"), as.Date("2014-12-31"))
gas_ts[dates]

returns data for the last days of June and December 2014 of the gas_ts object. Remember that the seq() function can be used for dates.

The xts package contains a set of functions that apply a function to non-overlapping periods, e.g., weekly, monthly, etc. For example,

apply.yearly(gas_ts, max)

returns the highest gas price in each year.

The gas_ts object is available in the environment.

This exercise is part of the course

Practicing Statistics Interview Questions in R

View Course

Hands-on interactive exercise

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

library(xts)

# Create the sequence of dates
dates_2014 <- ___(from = ___(___), ___ = as.Date("2014-12-31"), by = "1 day")

# Subset the time series
gas_2014 <- gas_ts[___]
Edit and Run Code