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.
Diese Übung ist Teil des Kurses
Case Study: Analyzing City Time Series Data in R
Anleitung zur Übung
- Subset your
temps_xts
object to include only observations from 2010 through 2015. Save this astemps_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 setOHLC
toFALSE
to avoid generating new OHLC columns. Finally, set theindexAt
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 theflights_xts
data from the previous chapter.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
# Subset your temperature data to include only 2010 through 2015: temps_xts_2
temps_xts_2 <- ___["___/___"]
# Use to.period to convert temps_xts_2 to monthly periodicity
temps_monthly <- to.period(___, period = "___", OHLC = ___, indexAt = "___")
# Compare the periodicity and duration of temps_monthly and flights_xts
periodicity(___)
periodicity(___)