Song of the Day

Main Ideas

Coming Up

Lecture Notes and Exercises

Load the tidyverse package. A package is just a bunch of shareable code.

library(tidyverse)

Follow the steps outlined in the slides to change “Your Name” in the header above to your actual name. Then, click the black arrow next to the “Knit” button and click “Knit to PDF”.

If you are satisfied with these changes, stage, commit, and push them following the steps in the slides.

Add a basic computation to the code chunk below (something like 2+2). Run the code chunk by clicking the green arrow on the right side code chunk. What do you notice?

2 + 2^2 + (10 / 2) + 4^3
## [1] 75

Practice

  1. Click the green arrow to run the code chunk below. You should see a visualization of highway miles per gallon versus displacement for 234 cars, with points colored by the type of drive train (f = front-wheel drive, r = rear wheel drive, 4 = 4wd).
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + 
  geom_point()

Question: How would you describe the relationship between type of drive train, displacement, and highway miles per gallon?

We first note (or google) that the displacement refers to the swept volume of all of the cylinders inside of an engine and is an important determinant of a car’s power.

Overall, higher displacement is associated with lower highway miles per gallon. This relationship is strong for lower levels of displacement, but levels off for higher displacement engines. The trend is strongest for four wheel and front wheel drive cars and weaker for rear wheel drive cars. Front wheel drive cars tend to have lower displacement and higher highway miles per gallon, rear wheel drive cars tend to have higher displacement with highway miles per gallon about between 20 and 30, and four wheel drive cars have the widest range of displacement.

  1. Create new code in the code chunk below to create a scatterplot of highway miles per gallon versus displacement with points colored by class (just replace drv with class).
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = class)) + 
  geom_point()

Question: How would you describe the relationship between type of drive train, displacement, and highway miles per gallon?

Heavy cars, like sports utility vehicles and pickups have a high displacement (powerful engines) and lower highway miles per gallon. Lighter cars, like subcompacts and compacts have low displacement (weaker engines) and higher highway miles per gallon.

The 2 seater cars are noteable. This class gets decent mileage considering their high displacement engines. These are smaller cars with powerful engines (sports cars).

When you are satisfied with your visualizations and answers, knit the document, then stage, commit, and push your changes.

Additional Resources