The following steps will use both plyr and the graphics library, ggplot2, to explore the dataset:
- Let's start by looking at whether there is an overall trend of how MPG changes over time on average. To do this, we use the ddply function from the plyr package to take the vehicles data frame, aggregate rows by year, and then, for each group, we compute the mean highway, city, and combine fuel efficiency. The result is then assigned to a new data frame, mpgByYr. Note that this is our first example of split-apply-combine. We split the data frame into groups by year, we apply the mean function to specific variables, and then we combine the results into a new data frame:
mpgByYr <- ddply(vehicles, ~year, summarise, avgMPG = ...