Session Ready
Exercise

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.

Instructions 1/3
undefined XP
  • 1
  • 2
  • 3
  • Create a vector with a sequence of dates for each day of the year 2014.
  • Create a subset of gas_ts for the year 2014.