Exercise

Subsetting and adjusting periodicity

Your next step is to merge your temperature data with the flight data from the previous chapter.

Recall from the previous chapter that your flight data stretches from 2010 through 2015 in monthly periods. By contrast, your temperature data ranges from 2007 through 2015 in daily periods. Before you merge, you should subset your data and adjust the periodicity to monthly.

To convert the periodicity of xts objects, you can use to.period(), which allows you to quickly convert your data to a lower frequency period. By default, this command produces specific values across the entire period (namely, Open-High-Low-Close, or OHLC) which are useful in financial analysis but may not be relevant in all contexts.

In this case, you should set the argument OHLC to FALSE. Rather than produce OHLC columns in your monthly xts object, this setting will simply take one row from each period as representative of the entire period. You can specify which row using the indexAt command.

Both the temps_xts data and the flights_xts data (from the previous chapter) are available in your workspace.

Instructions

100 XP
  • Subset your temps_xts object to include only observations from 2010 through 2015. Save this as temps_xts_2.
  • Use to.period() to convert your daily temperature data to monthly periodicity. Be sure to specify the period you'd like to convert to ("months"). You also need to set OHLC to FALSE to avoid generating new OHLC columns. Finally, set the indexAt argument to "firstof" to select the first observation each month.
  • Use two calls of periodicity() to compare the periodicity and duration of your new monthly temperature data to the flights_xts data from the previous chapter.