Get startedGet started for free

Extract weekend games

After calculating some useful indicators from your Red Sox data, it's time to step back and explore data from other Boston sports teams. Specifically, you've collected additional data on the New England Patriots (football), the Boston Bruins (hockey), and the Boston Celtics (basketball). Data for these teams, along with your redsox data, have been merged into a single xts object, sports, which now contains data on all games played by Boston-area sports teams from 2010 through 2015.

Before conducting further analysis, you want to refine your data into a few potentially useful subsets. In particular, it may be helpful to focus exclusively on weekend games involving Boston sports teams.

To identify games based on the day of the week, you should use the .indexwday() command, which tells you the day of the week of each observation in your xts object. These values range from 0-6, with Sunday equal to 0 and Saturday equal to 6.

This exercise is part of the course

Case Study: Analyzing City Time Series Data in R

View Course

Exercise instructions

  • Practice with the xts indexing commands by extracting the day of the week of each observation in your sports data using .indexwday(). Save these values to weekday and view the first few rows of this weekday object using head().
  • Create an index of weekend observations by combining the which() command with two calls to .indexwday(). The goal here is to extract only those dates falling on a Saturday or a Sunday. Save this index as weekend.
  • Generate a new xts object (weekend_games) containing only games falling on a weekend. Use head() to view the first few rows of your new weekend_games object.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Extract the day of the week of each observation
weekday <- .indexwday(___)
head(___)

# Generate an index of weekend dates
weekend <- which(.indexwday(___) == ___ | .indexwday(___) == ___)

# Subset only weekend games
weekend_games <- sports[___]
head(___)
Edit and Run Code