Calculate time series trends
One of the most useful qualities of xts objects is the ability to conduct simple mathematical equations across time. In your flight data, one valuable metric to calculate would be the percentage of flights delayed, cancelled, or diverted each month.
In this exercise, you'll use your data to generate a new time series column containing the percentage of flights arriving late to Boston each month. You'll then generate a plot for this metric, before going on to calculate additional metrics for flight cancellations and diversions.
This exercise is part of the course
Case Study: Analyzing City Time Series Data in R
Exercise instructions
- Use simple math expressions on
flights_xts
to calculate the percentage of flights delayed each month. Save this as a new column inflights_xts
calledpct_delay
. - Use
plot.xts()
to view the percent of flights delayed each month. - Replicate your calculation above to produce two additional columns of data in your xts object --
pct_cancel
andpct_divert
-- for cancelled and diverted flights, respectively. - Use
plot.zoo()
to view all three trends together. To do so, you'll need to select a subset of theflights_xts
data containing the three columns you just generated.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Calculate percentage of flights delayed each month: pct_delay
flights_xts$pct_delay <- (___ / ___) * 100
# Use plot.xts() to view pct_delay over time
# Calculate percentage of flights cancelled each month: pct_cancel
# Calculate percentage of flights diverted each month: pct_divert
# Use plot.zoo() to view all three trends over time
plot.zoo(x = ___[ , c("___", "___", "___")])