dplyr and ggplot2 Exercises

Heres the code and output for the set up including library(dplyr) so all the commands would work.

data(mtcars)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
mtcars<- mtcars%>%tibble::rownames_to_column()%>%rename("make_model"=rowname)

Now for the output and code for the first question using the commands we learned in class

filter(mtcars, gear=="4") %>% select(make_model,hp,gear) %>% arrange(desc(hp))
##        make_model  hp gear
## 1        Merc 280 123    4
## 2       Merc 280C 123    4
## 3       Mazda RX4 110    4
## 4   Mazda RX4 Wag 110    4
## 5      Volvo 142E 109    4
## 6        Merc 230  95    4
## 7      Datsun 710  93    4
## 8        Fiat 128  66    4
## 9       Fiat X1-9  66    4
## 10 Toyota Corolla  65    4
## 11      Merc 240D  62    4
## 12    Honda Civic  52    4

Here’s the select command for the second question.

select(mtcars,make_model,hp)
##             make_model  hp
## 1            Mazda RX4 110
## 2        Mazda RX4 Wag 110
## 3           Datsun 710  93
## 4       Hornet 4 Drive 110
## 5    Hornet Sportabout 175
## 6              Valiant 105
## 7           Duster 360 245
## 8            Merc 240D  62
## 9             Merc 230  95
## 10            Merc 280 123
## 11           Merc 280C 123
## 12          Merc 450SE 180
## 13          Merc 450SL 180
## 14         Merc 450SLC 180
## 15  Cadillac Fleetwood 205
## 16 Lincoln Continental 215
## 17   Chrysler Imperial 230
## 18            Fiat 128  66
## 19         Honda Civic  52
## 20      Toyota Corolla  65
## 21       Toyota Corona  97
## 22    Dodge Challenger 150
## 23         AMC Javelin 150
## 24          Camaro Z28 245
## 25    Pontiac Firebird 175
## 26           Fiat X1-9  66
## 27       Porsche 914-2  91
## 28        Lotus Europa 113
## 29      Ford Pantera L 264
## 30        Ferrari Dino 175
## 31       Maserati Bora 335
## 32          Volvo 142E 109

And finally for the average miles per gallon for an automatic car.

filter(mtcars, am=="0") %>% select(am,mpg) %>% summarise(sum(mpg)/19)
##   sum(mpg)/19
## 1    17.14737

And the average mpg for a manual.

filter(mtcars, am=="1") %>% select(am,mpg) %>% summarise(sum(mpg)/13)
##   sum(mpg)/13
## 1    24.39231