How to do it...

The following steps will use both plyr and the graphics library, ggplot2, to explore the dataset:

  1. 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 =  ...

Get Practical Data Science Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.