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.
Cet exercice fait partie du cours
Case Study: Analyzing City Time Series Data in R
Instructions
- Use
summary()
to view some summary statistics about yourredsox
data. Keep an eye out for the date column and assess whether or not you have missing data (NA
s) that needs addressing. - Once you're satisfied that the
redsox
data can be converted to xts, start this process by encoding thedate
column to a time-based object usingas.Date()
. - Use
as.xts()
to convert yourredsox
data to xts, being sure toorder.by
thedate
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?
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# View summary information about your redsox data
# Convert the date column to a time-based format
redsox$date <- as.Date(___$___)
# Convert your red sox data to xts
redsox_xts <- as.xts(___[,-1], order.by = ___$___)
# Plot the Red Sox score and the opponent score over time
plot.zoo(___[, c("___", "___")])