Session Ready
Exercise

Encoding and plotting Red Sox data

After exploring and manipulating data on flights, weather, and the economy, your client wants to cover all the bases. Naturally, they'd like you to collect data on Boston's major sports teams: the Boston Red Sox (baseball), the New England Patriots (football), the Boston Bruins (hockey), and the Boston Celtics (basketball). In this chapter, you'll prepare data on the schedule and outcome of all games involving these teams from 2010 through 2015. It's a perfect opportunity to gain further practice manipulating time series data!

As a start, you've compiled data on games played by the Boston Red Sox from 2010 through 2015. In this exercise, you'll explore the data, encode it to xts, and plot some trends over time. The redsox data frame is available in your workspace.

Instructions
100 XP
  • Use summary() to view some summary statistics about your redsox data. Keep an eye out for the date column and assess whether or not you have missing data (NAs) that needs addressing.
  • Once you're satisfied that the redsox data can be converted to xts, start this process by encoding the date column to a time-based object using as.Date().
  • Use as.xts() to convert your redsox data to xts, being sure to order.by the date column. Also remove the date column (using [, -1] notation) to ensure that your xts object is numeric.
  • Use plot.zoo() to plot Red Sox scores (boston_score) and opponent scores (opponent_score) over time. What trends can you identify from these plots?