Introduction

Get data from:

  1. http://www.fueleconomy.gov/feg/epadata/vehicles.csv.zip, and saved into the folder: “~/Andrew_Proj_Connected_Cars”"

  2. This file will describe the columns names: http://www.fueleconomy.gov/feg/ws/index.shtml#vehicle.

Note: Select and copy all the text below the vehicle heading under Data Description, and paste it into a text file, but not include the emissions heading. Save this file in your working directory as varlabels.txt. The first few lines of the file are as follows: atvtype - type of alternative fuel or advanced technology vehicle barrels08 - annual petroleum consumption in barrels for fuelType1 (1) barrelsA08 - annual petroleum consumption in barrels for fuelType2 (1) charge120 - time to charge an electric vehicle in hours at 120 V charge240 - time to charge an electric vehicle in hours at 240 V

Data Obtaining and Cleaning

I loaded the data directly from compressed (ZIP) files:the ZIP archive that you want to load: Packages used: plyr, ggplot2, reshape2

Part 1: Download and Importing automobile fuel efficiency data “vehicles.csv.zip” into R

## Warning: package 'plyr' was built under R version 3.1.2
## Warning: package 'dplyr' was built under R version 3.1.2
## 
## Attaching package: 'dplyr'
## 
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
## Warning: package 'ggplot2' was built under R version 3.1.2
## Warning: package 'reshape2' was built under R version 3.1.2
## [1] 35621    83
##   barrels08 barrelsA08 charge120 charge240 city08 city08U cityA08 cityA08U
## 1 15.689436        0.0         0         0     19       0       0        0
##   cityCD cityE cityUF co2 co2A co2TailpipeAGpm    co2TailpipeGpm comb08
## 1      0     0      0  -1   -1             0.0 423.1904761904762     21
##   comb08U combA08 combA08U combE combinedCD combinedUF cylinders displ
## 1       0       0        0     0          0          0         4     2
##              drive engId eng_dscr feScore fuelCost08 fuelCostA08 fuelType
## 1 Rear-Wheel Drive  9011    (FFS)      -1       2050           0  Regular
##          fuelType1 ghgScore ghgScoreA highway08 highway08U highwayA08
## 1 Regular Gasoline       -1        -1        25          0          0
##   highwayA08U highwayCD highwayE highwayUF hlv hpv id lv2 lv4       make
## 1           0         0        0         0   0   0  1   0   0 Alfa Romeo
##                model mpgData phevBlended pv2 pv4 range rangeCity
## 1 Spider Veloce 2000       Y       false   0   0     0         0
##   rangeCityA rangeHwy rangeHwyA        trany   UCity UCityA UHighway
## 1          0        0         0 Manual 5-spd 23.3333      0       35
##   UHighwayA      VClass year youSaveSpend guzzler trans_dscr tCharger
## 1         0 Two Seaters 1985        -1250                          NA
##   sCharger atvType fuelType2 rangeA evMotor mfrCode c240Dscr charge240b
## 1                                                                     0
##   c240bDscr                    createdOn                   modifiedOn
## 1           Tue Jan 01 00:00:00 EST 2013 Tue Jan 01 00:00:00 EST 2013
##   startStop phevCity phevHwy phevComb
## 1                  0       0        0
##  [1] "barrels08"       "barrelsA08"      "charge120"      
##  [4] "charge240"       "city08"          "city08U"        
##  [7] "cityA08"         "cityA08U"        "cityCD"         
## [10] "cityE"           "cityUF"          "co2"            
## [13] "co2A"            "co2TailpipeAGpm" "co2TailpipeGpm" 
## [16] "comb08"          "comb08U"         "combA08"        
## [19] "combA08U"        "combE"           "combinedCD"     
## [22] "combinedUF"      "cylinders"       "displ"          
## [25] "drive"           "engId"           "eng_dscr"       
## [28] "feScore"         "fuelCost08"      "fuelCostA08"    
## [31] "fuelType"        "fuelType1"       "ghgScore"       
## [34] "ghgScoreA"       "highway08"       "highway08U"     
## [37] "highwayA08"      "highwayA08U"     "highwayCD"      
## [40] "highwayE"        "highwayUF"       "hlv"            
## [43] "hpv"             "id"              "lv2"            
## [46] "lv4"             "make"            "model"          
## [49] "mpgData"         "phevBlended"     "pv2"            
## [52] "pv4"             "range"           "rangeCity"      
## [55] "rangeCityA"      "rangeHwy"        "rangeHwyA"      
## [58] "trany"           "UCity"           "UCityA"         
## [61] "UHighway"        "UHighwayA"       "VClass"         
## [64] "year"            "youSaveSpend"    "guzzler"        
## [67] "trans_dscr"      "tCharger"        "sCharger"       
## [70] "atvType"         "fuelType2"       "rangeA"         
## [73] "evMotor"         "mfrCode"         "c240Dscr"       
## [76] "charge240b"      "c240bDscr"       "createdOn"      
## [79] "modifiedOn"      "startStop"       "phevCity"       
## [82] "phevHwy"         "phevComb"
## 'data.frame':    35621 obs. of  83 variables:
##  $ barrels08      : chr  "15.689436" "29.950562" "12.19557" "29.950562" ...
##  $ barrelsA08     : chr  "0.0" "0.0" "0.0" "0.0" ...
##  $ charge120      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ charge240      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ city08         : int  19 9 23 10 17 21 22 23 23 23 ...
##  $ city08U        : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ cityA08        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ cityA08U       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ cityCD         : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ cityE          : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ cityUF         : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ co2            : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ co2A           : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ co2TailpipeAGpm: chr  "0.0" "0.0" "0.0" "0.0" ...
##  $ co2TailpipeGpm : chr  "423.1904761904762" "807.9090909090909" "329.14814814814815" "807.9090909090909" ...
##  $ comb08         : int  21 11 27 11 19 22 25 24 26 25 ...
##  $ comb08U        : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ combA08        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ combA08U       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ combE          : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ combinedCD     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ combinedUF     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ cylinders      : int  4 12 4 8 4 4 4 4 4 4 ...
##  $ displ          : num  2 4.9 2.2 5.2 2.2 1.8 1.8 1.6 1.6 1.8 ...
##  $ drive          : chr  "Rear-Wheel Drive" "Rear-Wheel Drive" "Front-Wheel Drive" "Rear-Wheel Drive" ...
##  $ engId          : int  9011 22020 2100 2850 66031 66020 66020 57005 57005 57006 ...
##  $ eng_dscr       : chr  "(FFS)" "(GUZZLER)" "(FFS)" "" ...
##  $ feScore        : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ fuelCost08     : int  2050 3950 1600 3950 2600 1950 1750 1800 1650 1750 ...
##  $ fuelCostA08    : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ fuelType       : chr  "Regular" "Regular" "Regular" "Regular" ...
##  $ fuelType1      : chr  "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" "Regular Gasoline" ...
##  $ ghgScore       : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ ghgScoreA      : int  -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...
##  $ highway08      : int  25 14 33 12 23 24 29 26 31 30 ...
##  $ highway08U     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ highwayA08     : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ highwayA08U    : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ highwayCD      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ highwayE       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ highwayUF      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ hlv            : int  0 0 19 0 0 0 0 0 0 0 ...
##  $ hpv            : int  0 0 77 0 0 0 0 0 0 0 ...
##  $ id             : int  1 10 100 1000 10000 10001 10002 10003 10004 10005 ...
##  $ lv2            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ lv4            : int  0 0 0 0 14 15 15 13 13 13 ...
##  $ make           : chr  "Alfa Romeo" "Ferrari" "Dodge" "Dodge" ...
##  $ model          : chr  "Spider Veloce 2000" "Testarossa" "Charger" "B150/B250 Wagon 2WD" ...
##  $ mpgData        : chr  "Y" "N" "Y" "N" ...
##  $ phevBlended    : chr  "false" "false" "false" "false" ...
##  $ pv2            : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ pv4            : int  0 0 0 0 90 88 88 89 89 89 ...
##  $ range          : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ rangeCity      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ rangeCityA     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ rangeHwy       : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ rangeHwyA      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ trany          : chr  "Manual 5-spd" "Manual 5-spd" "Manual 5-spd" "Automatic 3-spd" ...
##  $ UCity          : num  23.3 11 29 12.2 21 ...
##  $ UCityA         : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ UHighway       : num  35 19 47 16.7 32 ...
##  $ UHighwayA      : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ VClass         : chr  "Two Seaters" "Two Seaters" "Subcompact Cars" "Vans" ...
##  $ year           : int  1985 1985 1985 1985 1993 1993 1993 1993 1993 1993 ...
##  $ youSaveSpend   : int  -1250 -10750 1000 -10750 -4000 -750 250 0 750 250 ...
##  $ guzzler        : chr  "" "T" "" "" ...
##  $ trans_dscr     : chr  "" "" "SIL" "" ...
##  $ tCharger       : logi  NA NA NA NA TRUE NA ...
##  $ sCharger       : chr  "" "" "" "" ...
##  $ atvType        : chr  "" "" "" "" ...
##  $ fuelType2      : chr  "" "" "" "" ...
##  $ rangeA         : chr  "" "" "" "" ...
##  $ evMotor        : chr  "" "" "" "" ...
##  $ mfrCode        : chr  "" "" "" "" ...
##  $ c240Dscr       : chr  "" "" "" "" ...
##  $ charge240b     : num  0 0 0 0 0 0 0 0 0 0 ...
##  $ c240bDscr      : chr  "" "" "" "" ...
##  $ createdOn      : chr  "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" ...
##  $ modifiedOn     : chr  "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" "Tue Jan 01 00:00:00 EST 2013" ...
##  $ startStop      : chr  "" "" "" "" ...
##  $ phevCity       : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ phevHwy        : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ phevComb       : int  0 0 0 0 0 0 0 0 0 0 ...

Part 2: Download and Importing automobile Descriptionfuel “variabels.txt” into R.

The format of the variabels.txt is looking like:

atvtype - type of alternative fuel or advanced technology vehicle barrels08 - annual petroleum consumption in barrels for fuelType1 (1) …

I need to convert it into the format like this:

“atvtype” “type of alternative fuel or advanced technology vehicle”
“barrels08” “annual petroleum consumption in barrels for fuelType1 (1)” …

# This one have error, because there are two - symbols in line 11 of the data file
# labels <- read.table("variabels.txt", sep = "-", header = FALSE)

# Should change our file reading to ignore hyphenated words:
lables <- do.call(rbind, strsplit(readLines("variabels.txt"), " - "))
## Warning in readLines("variabels.txt"): incomplete final line found on
## 'variabels.txt'
lables
##       [,1]                                                                
##  [1,] "atvtype"                                                           
##  [2,] "barrels08"                                                         
##  [3,] "barrelsA08"                                                        
##  [4,] "charge120"                                                         
##  [5,] "charge240"                                                         
##  [6,] "city08"                                                            
##  [7,] "city08U"                                                           
##  [8,] "cityA08"                                                           
##  [9,] "cityA08U"                                                          
## [10,] "cityCD"                                                            
## [11,] "cityE"                                                             
## [12,] "cityUF"                                                            
## [13,] "co2"                                                               
## [14,] "co2A"                                                              
## [15,] "co2TailpipeAGpm"                                                   
## [16,] "co2TailpipeGpm- tailpipe CO2 in grams/mile for fuelType1 (5)"      
## [17,] "comb08"                                                            
## [18,] "comb08U"                                                           
## [19,] "combA08"                                                           
## [20,] "combA08U"                                                          
## [21,] "combE"                                                             
## [22,] "combinedCD"                                                        
## [23,] "combinedUF"                                                        
## [24,] "cylinders"                                                         
## [25,] "displ"                                                             
## [26,] "drive"                                                             
## [27,] "emissionsList"                                                     
## [28,] "engId"                                                             
## [29,] "eng_dscr"                                                          
## [30,] "evMotor"                                                           
## [31,] "feScore"                                                           
## [32,] "fuelCost08"                                                        
## [33,] "fuelCostA08"                                                       
## [34,] "fuelType"                                                          
## [35,] "fuelType1"                                                         
## [36,] "fuelType2"                                                         
## [37,] "ghgScore"                                                          
## [38,] "ghgScoreA"                                                         
## [39,] "guzzler- if G or T, this vehicle is subject to the gas guzzler tax"
## [40,] "highway08"                                                         
## [41,] "highway08U"                                                        
## [42,] "highwayA08"                                                        
## [43,] "highwayA08U"                                                       
## [44,] "highwayCD"                                                         
## [45,] "highwayE"                                                          
## [46,] "highwayUF"                                                         
## [47,] "hlv"                                                               
## [48,] "hpv"                                                               
## [49,] "id"                                                                
## [50,] "lv2"                                                               
## [51,] "lv4"                                                               
## [52,] "make"                                                              
## [53,] "mfrCode"                                                           
## [54,] "model"                                                             
## [55,] "mpgData"                                                           
## [56,] "phevBlended"                                                       
## [57,] "pv2"                                                               
## [58,] "pv4"                                                               
## [59,] "rangeA"                                                            
## [60,] "rangeCityA"                                                        
## [61,] "rangeHwyA"                                                         
## [62,] "trans_dscr"                                                        
## [63,] "trany"                                                             
## [64,] "UCity"                                                             
## [65,] "UCityA"                                                            
## [66,] "UHighway"                                                          
## [67,] "UHighwayA"                                                         
## [68,] "VClass"                                                            
## [69,] "year"                                                              
## [70,] "youSaveSpend"                                                      
## [71,] "sCharger"                                                          
## [72,] "tCharger"                                                          
## [73,] "c240Dscr"                                                          
## [74,] "charge240b"                                                        
## [75,] "c240bDscr"                                                         
## [76,] "createdOn"                                                         
## [77,] "modifiedOn"                                                        
## [78,] "startStop"                                                         
## [79,] "phevCity"                                                          
## [80,] "phevHwy"                                                           
## [81,] "phevComb"                                                          
##       [,2]                                                                                                                                                                                              
##  [1,] "type of alternative fuel or advanced technology vehicle"                                                                                                                                         
##  [2,] "annual petroleum consumption in barrels for fuelType1 (1)"                                                                                                                                       
##  [3,] "annual petroleum consumption in barrels for fuelType2 (1)"                                                                                                                                       
##  [4,] "time to charge an electric vehicle in hours at 120 V"                                                                                                                                            
##  [5,] "time to charge an electric vehicle in hours at 240 V"                                                                                                                                            
##  [6,] "city MPG for fuelType1 (2)"                                                                                                                                                                      
##  [7,] "unrounded city MPG for fuelType1 (2), (3)"                                                                                                                                                       
##  [8,] "city MPG for fuelType2 (2)"                                                                                                                                                                      
##  [9,] "unrounded city MPG for fuelType2 (2), (3)"                                                                                                                                                       
## [10,] "city gasoline consumption (gallons/100 miles) in charge depleting mode (4)"                                                                                                                      
## [11,] "city electricity consumption in kw-hrs/100 miles"                                                                                                                                                
## [12,] "EPA city utility factor (share of electricity) for PHEV"                                                                                                                                         
## [13,] "tailpipe CO2 in grams/mile for fuelType1 (5)"                                                                                                                                                    
## [14,] "tailpipe CO2 in grams/mile for fuelType2 (5)"                                                                                                                                                    
## [15,] "tailpipe CO2 in grams/mile for fuelType2 (5)"                                                                                                                                                    
## [16,] "co2TailpipeGpm- tailpipe CO2 in grams/mile for fuelType1 (5)"                                                                                                                                    
## [17,] "combined MPG for fuelType1 (2)"                                                                                                                                                                  
## [18,] "unrounded combined MPG for fuelType1 (2), (3)"                                                                                                                                                   
## [19,] "combined MPG for fuelType2 (2)"                                                                                                                                                                  
## [20,] "unrounded combined MPG for fuelType2 (2), (3)"                                                                                                                                                   
## [21,] "combined electricity consumption in kw-hrs/100 miles"                                                                                                                                            
## [22,] "combined gasoline consumption (gallons/100 miles) in charge depleting mode (4)"                                                                                                                  
## [23,] "EPA combined utility factor (share of electricity) for PHEV"                                                                                                                                     
## [24,] "engine cylinders"                                                                                                                                                                                
## [25,] "engine displacement in liters"                                                                                                                                                                   
## [26,] "drive axle type"                                                                                                                                                                                 
## [27,] "emissionsList"                                                                                                                                                                                   
## [28,] "EPA model type index"                                                                                                                                                                            
## [29,] "engine descriptor; see http://www.fueleconomy.gov/feg/findacarhelp.shtml#engine"                                                                                                                 
## [30,] "electric motor (kw-hrs)"                                                                                                                                                                         
## [31,] "EPA Fuel Economy Score (-1 = Not available)"                                                                                                                                                     
## [32,] "annual fuel cost for fuelType1 ($) (7)"                                                                                                                                                          
## [33,] "annual fuel cost for fuelType2 ($) (7)"                                                                                                                                                          
## [34,] "fuel type with fuelType1 and fuelType2 (if applicable)"                                                                                                                                          
## [35,] "fuel type 1. For single fuel vehicles, this will be the only fuel. For dual fuel vehicles, this will be the conventional fuel."                                                                  
## [36,] "fuel type 2. For dual fuel vehicles, this will be the alternative fuel (e.g. E85, Electricity, CNG, LPG). For single fuel vehicles, this field is not used"                                      
## [37,] "EPA GHG score (-1 = Not available)"                                                                                                                                                              
## [38,] "EPA GHG score for dual fuel vehicle running on the alternative fuel (-1 = Not available)"                                                                                                        
## [39,] "guzzler- if G or T, this vehicle is subject to the gas guzzler tax"                                                                                                                              
## [40,] "highway MPG for fuelType1 (2)"                                                                                                                                                                   
## [41,] "unrounded highway MPG for fuelType1 (2), (3)"                                                                                                                                                    
## [42,] "highway MPG for fuelType2 (2)"                                                                                                                                                                   
## [43,] "unrounded highway MPG for fuelType2 (2),(3)"                                                                                                                                                     
## [44,] "highway gasoline consumption (gallons/100miles) in charge depleting mode (4)"                                                                                                                    
## [45,] "highway electricity consumption in kw-hrs/100 miles"                                                                                                                                             
## [46,] "EPA highway utility factor (share of electricity) for PHEV"                                                                                                                                      
## [47,] "hatchback luggage volume (cubic feet) (8)"                                                                                                                                                       
## [48,] "hatchback passenger volume (cubic feet) (8)"                                                                                                                                                     
## [49,] "vehicle record id"                                                                                                                                                                               
## [50,] "2 door luggage volume (cubic feet) (8)"                                                                                                                                                          
## [51,] "4 door luggage volume (cubic feet) (8)"                                                                                                                                                          
## [52,] "manufacturer (division)"                                                                                                                                                                         
## [53,] "3-character manufacturer code"                                                                                                                                                                   
## [54,] "model name (carline)"                                                                                                                                                                            
## [55,] "has My MPG data; see yourMpgVehicle and yourMpgDriverVehicle"                                                                                                                                    
## [56,] "if true, this vehicle operates on a blend of gasoline and electricity in charge depleting mode"                                                                                                  
## [57,] "2-door passenger volume (cubic feet) (8)"                                                                                                                                                        
## [58,] "4-door passenger volume (cubic feet) (8)"                                                                                                                                                        
## [59,] "EPA range for fuelType2"                                                                                                                                                                         
## [60,] "EPA city range for fuelType2"                                                                                                                                                                    
## [61,] "EPA highway range for fuelType2"                                                                                                                                                                 
## [62,] "transmission descriptor; see http://www.fueleconomy.gov/feg/findacarhelp.shtml#trany"                                                                                                            
## [63,] "transmission"                                                                                                                                                                                    
## [64,] "unadjusted city MPG for fuelType1; see the description of the EPA test procedures"                                                                                                               
## [65,] "unadjusted city MPG for fuelType2; see the description of the EPA test procedures"                                                                                                               
## [66,] "unadjusted highway MPG for fuelType1; see the description of the EPA test procedures"                                                                                                            
## [67,] "unadjusted highway MPG for fuelType2; see the description of the EPA test procedures"                                                                                                            
## [68,] "EPA vehicle size class"                                                                                                                                                                          
## [69,] "model year"                                                                                                                                                                                      
## [70,] "you save/spend over 5 years compared to an average car ($). Savings are positive; a greater amount spent yields a negative number. For dual fuel vehicles, this is the cost savings for gasoline"
## [71,] "if S, this vehicle is supercharged"                                                                                                                                                              
## [72,] "if T, this vehicle is turbocharged"                                                                                                                                                              
## [73,] "electric vehicle charger description"                                                                                                                                                            
## [74,] "time to charge an electric vehicle in hours at 240 V using the alternate charger"                                                                                                                
## [75,] "electric vehicle alternate charger description"                                                                                                                                                  
## [76,] "date the vehicle record was created (ISO 8601 format)"                                                                                                                                           
## [77,] "date the vehicle record was last modified (ISO 8601 format)"                                                                                                                                     
## [78,] "vehicle has start-stop technology (Y, N, or blank for older vehicles)"                                                                                                                           
## [79,] "EPA composite gasoline-electricity city MPGe for plug-in hybrid vehicles"                                                                                                                        
## [80,] "EPA composite gasoline-electricity highway MPGe for plug-in hybrid vehicles"                                                                                                                     
## [81,] "EPA composite gasoline-electricity combined city-highway MPGe for plug-in hybrid vehicles"
#head(lables, 2)
dim(lables)
## [1] 81  2

Data Exploration Part One: Fuel Efficiency

1. Find out what types of fuel are used as the automobiles’ primary fuel types:

table(vehicles$fuelType1)
## 
##            Diesel       Electricity Midgrade Gasoline       Natural Gas 
##              1064                71                54                60 
##  Premium Gasoline  Regular Gasoline 
##              9134             25238

From this, we can see that most cars in the dataset use regular gasoline, and the second most common fuel type is premium gasoline

2. The Years Coverd in the Data Set

first_year <- min(vehicles[, "year"])
last_year <- max(vehicles[, "year"])
#First Year
first_year 
## [1] 1984
#Last Year
last_year
## [1] 2015
# 
length(unique(vehicles$year))
## [1] 32
length(unique(vehicles[, "year"]))
## [1] 32

This shows the car data is from year 1984 to 2015, it covers 33 (32+1) years (including startuing and ending years)

3. Explore the types of transmissions used by these automobiles.

Foud some blanks in rows for the “trany” - transmission, so we need to take care of all missing data by setting it to NA first.

As we only care Automatic or Manual, so we can use substr function to extract the first four characters of each trany from: substr function to extract the first four characters of each trany (otherwise, it’s Manual):

Manual 5-spd, Automatic 4-spd, Manual 4-spd, etc

vehicles$trany[vehicles$trany == ""] <- NA
vehicles$trany2 <- ifelse(substr(vehicles$trany, 1, 4) == "Auto", "Auto", "Manual")
vehicles$trany <- as.factor(vehicles$trany)
table(vehicles$trany2)
## 
##   Auto Manual 
##  23566  12044
with(vehicles, table(sCharger, year))
##         year
## sCharger 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
##          1964 1701 1210 1247 1130 1149 1074 1130 1116 1088  979  962  767
##        S    0    0    0    0    0    4    4    2    5    5    3    5    6
##         year
## sCharger 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
##           757  800  840  826  891  949 1015 1089 1136 1067 1098 1152 1165
##        S    5   12   12   14   20   26   29   33   30   37   28   35   19
##         year
## sCharger 2010 2011 2012 2013 2014 2015
##          1091 1105 1125 1143 1161 1125
##        S   18   25   28   42   65   57

Data shows Automatic is almost doubled than Manual cars.

Analyzing automobile fuel efficiency over time

1. Let’s start by looking at whether there is an overall trend of how MPG changes over time on an average

# Get the new data set for the average mpg arrocss the years:
mpgByYear <- ddply(vehicles, ~year, summarise, avgMPG = mean(comb08), avgHghy = mean(highway08), avgCity = mean(city08))

ggplot(mpgByYear, aes(year, avgMPG)) + geom_point() + geom_smooth() + xlab("Year") + ylab("Average MPG") + ggtitle("Fig 1: MPG Improves over the Years")
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

table(vehicles$fuelType1)
## 
##            Diesel       Electricity Midgrade Gasoline       Natural Gas 
##              1064                71                54                60 
##  Premium Gasoline  Regular Gasoline 
##              9134             25238
vehicles_2011 <- subset(vehicles, year == 2011)
vehicles_2012 <- subset(vehicles, year == 2012)
vehicles_2014 <- subset(vehicles, year == 2014)
vehicles_2013 <- subset(vehicles, year == 2013)


fuelType1_factor_2011 <- as.factor(vehicles_2011$fuelType1)
fuelType1_factor_2012 <- as.factor(vehicles_2012$fuelType1)
fuelType1_factor_2013 <- as.factor(vehicles_2013$fuelType1)
fuelType1_factor_2014 <- as.factor(vehicles_2014$fuelType1)

plot(fuelType1_factor_2011)

#plot(fuelType1_factor_2012)
#plot(fuelType1_factor_2013)
plot(fuelType1_factor_2014)

# Get gas car data set:

gasCars <- subset(vehicles, fuelType1 %in% c("Regular Gasoline", "Premium Gasoline", "Midgrade Gasoline") & fuelType2 == "" & atvType != "Hybrid")

mpgByYear_Gas <- ddply(gasCars, ~year, summarise, avgMPG = mean(comb08))
ggplot(mpgByYear_Gas, aes(year, avgMPG)) + geom_point() + geom_smooth() +
xlab("Year") + ylab("Average MPG") + ggtitle("Fig 2: Gasoline cars")
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

We do see the mpg improved recently.

2. Find out the trend of the large engine cars been made recently

typeof(gasCars$displ)
## [1] "double"
gasCars$displ <- as.numeric(gasCars$displ)

ggplot(gasCars, aes(displ, comb08)) + geom_point() + geom_smooth() + ggtitle("Fig 3: Trend of Large Engines") +  xlab("Engine Displacement in Liters") + ylab("Combined MPG")
## geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
## Warning: Removed 2 rows containing missing values (stat_smooth).
## Warning: Removed 2 rows containing missing values (geom_point).

3. Find out if the small car made recently is the primary reason for in the fuel effiency

avgCarSize <- ddply(gasCars, ~year, summarise, avgDispl = mean(displ))
ggplot(avgCarSize, aes(year, avgDispl)) + geom_point() + geom_smooth() +
xlab("Year") + ylab("Average engine displacement (l)") + ggtitle("Fig 4: Trend of Engines Size") 
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 1 rows containing missing values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).

4. Finding out if smaller engine is the main reason for the better MPG

byYear <- ddply(gasCars, ~year, summarise, avgMPG = mean(comb08), avgDispl = mean(displ))
head(byYear)
##   year   avgMPG avgDispl
## 1 1984 19.12162 3.068449
## 2 1985 19.39469       NA
## 3 1986 19.32046 3.126514
## 4 1987 19.16457 3.096474
## 5 1988 19.36761 3.113558
## 6 1989 19.14196 3.133393
byYear2 = melt(byYear, id = "year")
head(byYear2)
##   year variable    value
## 1 1984   avgMPG 19.12162
## 2 1985   avgMPG 19.39469
## 3 1986   avgMPG 19.32046
## 4 1987   avgMPG 19.16457
## 5 1988   avgMPG 19.36761
## 6 1989   avgMPG 19.14196
ggplot(byYear2, aes(year, value)) + geom_point() + geom_smooth() +
facet_wrap(~variable, ncol = 1, scales = "free_y") + xlab("Year") + ylab("")
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 1 rows containing missing values (stat_smooth).
## Warning: Removed 1 rows containing missing values (geom_point).

5. Finding if four cylinder manual engines are more efficient over the years

gasCars4 <- subset(gasCars, cylinders == "4")
head(gasCars4)
##            barrels08 barrelsA08 charge120 charge240 city08 city08U cityA08
## 1          15.689436        0.0         0         0     19       0       0
## 3           12.19557        0.0         0         0     23       0       0
## 5 17.337486000000002        0.0         0         0     17       0       0
## 6          14.964294        0.0         0         0     21       0       0
## 7            13.1844        0.0         0         0     22       0       0
## 8           13.73375        0.0         0         0     23       0       0
##   cityA08U cityCD cityE cityUF co2 co2A co2TailpipeAGpm     co2TailpipeGpm
## 1        0      0     0      0  -1   -1             0.0  423.1904761904762
## 3        0      0     0      0  -1   -1             0.0 329.14814814814815
## 5        0      0     0      0  -1   -1             0.0  467.7368421052632
## 6        0      0     0      0  -1   -1             0.0 403.95454545454544
## 7        0      0     0      0  -1   -1             0.0             355.48
## 8        0      0     0      0  -1   -1             0.0  370.2916666666667
##   comb08 comb08U combA08 combA08U combE combinedCD combinedUF cylinders
## 1     21       0       0        0     0          0          0         4
## 3     27       0       0        0     0          0          0         4
## 5     19       0       0        0     0          0          0         4
## 6     22       0       0        0     0          0          0         4
## 7     25       0       0        0     0          0          0         4
## 8     24       0       0        0     0          0          0         4
##   displ                      drive engId   eng_dscr feScore fuelCost08
## 1   2.0           Rear-Wheel Drive  9011      (FFS)      -1       2050
## 3   2.2          Front-Wheel Drive  2100      (FFS)      -1       1600
## 5   2.2 4-Wheel or All-Wheel Drive 66031 (FFS,TRBO)      -1       2600
## 6   1.8          Front-Wheel Drive 66020      (FFS)      -1       1950
## 7   1.8          Front-Wheel Drive 66020      (FFS)      -1       1750
## 8   1.6          Front-Wheel Drive 57005      (FFS)      -1       1800
##   fuelCostA08 fuelType        fuelType1 ghgScore ghgScoreA highway08
## 1           0  Regular Regular Gasoline       -1        -1        25
## 3           0  Regular Regular Gasoline       -1        -1        33
## 5           0  Premium Premium Gasoline       -1        -1        23
## 6           0  Regular Regular Gasoline       -1        -1        24
## 7           0  Regular Regular Gasoline       -1        -1        29
## 8           0  Regular Regular Gasoline       -1        -1        26
##   highway08U highwayA08 highwayA08U highwayCD highwayE highwayUF hlv hpv
## 1          0          0           0         0        0         0   0   0
## 3          0          0           0         0        0         0  19  77
## 5          0          0           0         0        0         0   0   0
## 6          0          0           0         0        0         0   0   0
## 7          0          0           0         0        0         0   0   0
## 8          0          0           0         0        0         0   0   0
##      id lv2 lv4       make              model mpgData phevBlended pv2 pv4
## 1     1   0   0 Alfa Romeo Spider Veloce 2000       Y       false   0   0
## 3   100   0   0      Dodge            Charger       Y       false   0   0
## 5 10000   0  14     Subaru   Legacy AWD Turbo       N       false   0  90
## 6 10001   0  15     Subaru             Loyale       N       false   0  88
## 7 10002   0  15     Subaru             Loyale       Y       false   0  88
## 8 10003   0  13     Toyota            Corolla       Y       false   0  89
##   range rangeCity rangeCityA rangeHwy rangeHwyA           trany   UCity
## 1     0         0          0        0         0    Manual 5-spd 23.3333
## 3     0         0          0        0         0    Manual 5-spd 29.0000
## 5     0         0          0        0         0    Manual 5-spd 21.0000
## 6     0         0          0        0         0 Automatic 3-spd 27.0000
## 7     0         0          0        0         0    Manual 5-spd 28.0000
## 8     0         0          0        0         0 Automatic 3-spd 29.0000
##   UCityA UHighway UHighwayA          VClass year youSaveSpend guzzler
## 1      0       35         0     Two Seaters 1985        -1250        
## 3      0       47         0 Subcompact Cars 1985         1000        
## 5      0       32         0    Compact Cars 1993        -4000        
## 6      0       33         0    Compact Cars 1993         -750        
## 7      0       41         0    Compact Cars 1993          250        
## 8      0       37         0    Compact Cars 1993            0        
##   trans_dscr tCharger sCharger atvType fuelType2 rangeA evMotor mfrCode
## 1                  NA                                                  
## 3        SIL       NA                                                  
## 5                TRUE                                                  
## 6                  NA                                                  
## 7                  NA                                                  
## 8                  NA                                                  
##   c240Dscr charge240b c240bDscr                    createdOn
## 1                   0           Tue Jan 01 00:00:00 EST 2013
## 3                   0           Tue Jan 01 00:00:00 EST 2013
## 5                   0           Tue Jan 01 00:00:00 EST 2013
## 6                   0           Tue Jan 01 00:00:00 EST 2013
## 7                   0           Tue Jan 01 00:00:00 EST 2013
## 8                   0           Tue Jan 01 00:00:00 EST 2013
##                     modifiedOn startStop phevCity phevHwy phevComb trany2
## 1 Tue Jan 01 00:00:00 EST 2013                  0       0        0 Manual
## 3 Tue Jan 01 00:00:00 EST 2013                  0       0        0 Manual
## 5 Tue Jan 01 00:00:00 EST 2013                  0       0        0 Manual
## 6 Tue Jan 01 00:00:00 EST 2013                  0       0        0   Auto
## 7 Tue Jan 01 00:00:00 EST 2013                  0       0        0 Manual
## 8 Tue Jan 01 00:00:00 EST 2013                  0       0        0   Auto
ggplot(gasCars4, aes(factor(year), comb08)) + geom_boxplot() + facet_wrap(~trany2,
ncol = 1) + theme(axis.text.x = element_text(angle = 45)) + labs(x = "Year", y = "MPG")

6. Finding changes in proportion of manual cars available each year:

ggplot(gasCars4, aes(factor(year), fill = factor(trany2))) + geom_bar(position =
"fill") + labs(x = "Year", y = "Proportion of cars", fill = "Transmission") +
theme(axis.text.x = element_text(angle = 45)) + geom_hline(yintercept = 0.5,
linetype = 2)

Finding the makes and models of automobiles

1. Finding out makes and models over the years:

carsMake <- ddply(gasCars4, ~year, summarise, numberOfMakes = length(unique(make)))
ggplot(carsMake, aes(year, numberOfMakes)) + geom_point() + labs(x = "Year", y =
"Number of available makes") + ggtitle("Fig 8: Four cylinder cars")

2. Finding out makes and models over the years:

uniqMakes <- dlply(gasCars4, ~year, function(x) unique(x$make))
uniqMakes
## $`1984`
##  [1] "Alfa Romeo"                    "Bertone"                      
##  [3] "Ford"                          "Honda"                        
##  [5] "Lotus"                         "Pininfarina"                  
##  [7] "Pontiac"                       "Porsche"                      
##  [9] "Toyota"                        "Volkswagen"                   
## [11] "Audi"                          "BMW"                          
## [13] "Chevrolet"                     "Chrysler"                     
## [15] "Nissan"                        "Dodge"                        
## [17] "Isuzu"                         "Mercury"                      
## [19] "Mazda"                         "Mercedes-Benz"                
## [21] "Mitsubishi"                    "Plymouth"                     
## [23] "Renault"                       "Subaru"                       
## [25] "Buick"                         "Cadillac"                     
## [27] "Bill Dovell Motor Car Company" "Oldsmobile"                   
## [29] "Peugeot"                       "Saab"                         
## [31] "Volvo"                         "GMC"                          
## [33] "Import Foreign Auto Sales Inc" "Jeep"                         
## [35] "Suzuki"                        "AM General"                   
## [37] "American Motors Corporation"  
## 
## $`1985`
##  [1] "Alfa Romeo"                    "Dodge"                        
##  [3] "GMC"                           "Toyota"                       
##  [5] "Volkswagen"                    "AM General"                   
##  [7] "Chevrolet"                     "Jeep"                         
##  [9] "Plymouth"                      "Isuzu"                        
## [11] "Mitsubishi"                    "Subaru"                       
## [13] "Suzuki"                        "Nissan"                       
## [15] "Ford"                          "Honda"                        
## [17] "Chrysler"                      "Mercury"                      
## [19] "Mazda"                         "Pontiac"                      
## [21] "Buick"                         "Cadillac"                     
## [23] "Oldsmobile"                    "Volvo"                        
## [25] "Mercedes-Benz"                 "Bertone"                      
## [27] "Lotus"                         "Renault"                      
## [29] "Pininfarina"                   "Bill Dovell Motor Car Company"
## [31] "Porsche"                       "Merkur"                       
## [33] "Peugeot"                       "Saab"                         
## [35] "Audi"                          "BMW"                          
## 
## $`1986`
##  [1] "Alfa Romeo"                  "Bertone"                    
##  [3] "Ford"                        "Honda"                      
##  [5] "Panther Car Company Limited" "Pontiac"                    
##  [7] "Subaru"                      "Toyota"                     
##  [9] "Porsche"                     "Volkswagen"                 
## [11] "Yugo"                        "Audi"                       
## [13] "Chevrolet"                   "Chrysler"                   
## [15] "Nissan"                      "Dodge"                      
## [17] "Acura"                       "Hyundai"                    
## [19] "Isuzu"                       "Mercury"                    
## [21] "Mercedes-Benz"               "Mitsubishi"                 
## [23] "Plymouth"                    "Renault"                    
## [25] "Buick"                       "Cadillac"                   
## [27] "Merkur"                      "Mazda"                      
## [29] "Oldsmobile"                  "Peugeot"                    
## [31] "Saab"                        "Volvo"                      
## [33] "London Coach Co Inc"         "GMC"                        
## [35] "Jeep"                        "Suzuki"                     
## [37] "Volga Associated Automobile"
## 
## $`1987`
##  [1] "Ford"           "Mazda"          "Alfa Romeo"     "Bertone"       
##  [5] "CX Automotive"  "Honda"          "Lotus"          "Pontiac"       
##  [9] "Red Shift Ltd." "Subaru"         "Toyota"         "Porsche"       
## [13] "Volkswagen"     "Audi"           "Chevrolet"      "Chrysler"      
## [17] "Nissan"         "Dodge"          "Acura"          "Hyundai"       
## [21] "Isuzu"          "Mercedes-Benz"  "Mitsubishi"     "Plymouth"      
## [25] "Renault"        "Saab"           "Yugo"           "Buick"         
## [29] "Cadillac"       "Mercury"        "Merkur"         "Mcevoy Motors" 
## [33] "Oldsmobile"     "Peugeot"        "Volvo"          "GMC"           
## [37] "Jeep"           "Suzuki"        
## 
## $`1988`
##  [1] "Chrysler"                    "Ford"                       
##  [3] "Mercury"                     "Alfa Romeo"                 
##  [5] "Bertone"                     "Honda"                      
##  [7] "Lotus"                       "Maserati"                   
##  [9] "Panther Car Company Limited" "Pontiac"                    
## [11] "Red Shift Ltd."              "Subaru"                     
## [13] "Toyota"                      "Dacia"                      
## [15] "Porsche"                     "Volkswagen"                 
## [17] "Acura"                       "Audi"                       
## [19] "BMW"                         "Chevrolet"                  
## [21] "Nissan"                      "Dodge"                      
## [23] "Hyundai"                     "Isuzu"                      
## [25] "Mercedes-Benz"               "Mitsubishi"                 
## [27] "Plymouth"                    "Saab"                       
## [29] "Yugo"                        "Buick"                      
## [31] "Merkur"                      "Mazda"                      
## [33] "Mcevoy Motors"               "Oldsmobile"                 
## [35] "Peugeot"                     "Volvo"                      
## [37] "CX Automotive"               "Eagle"                      
## [39] "GMC"                         "Jeep"                       
## [41] "Grumman Allied Industries"   "Suzuki"                     
## 
## $`1989`
##  [1] "Alfa Romeo"    "Bertone"       "Chrysler"      "Honda"        
##  [5] "Lotus"         "Subaru"        "Toyota"        "Porsche"      
##  [9] "Volkswagen"    "Acura"         "Audi"          "BMW"          
## [13] "Daihatsu"      "Nissan"        "Dodge"         "Ford"         
## [17] "Geo"           "Hyundai"       "Isuzu"         "Mitsubishi"   
## [21] "Plymouth"      "Pontiac"       "Saab"          "Suzuki"       
## [25] "Yugo"          "Buick"         "Chevrolet"     "Eagle"        
## [29] "Mercury"       "Merkur"        "Mazda"         "Oldsmobile"   
## [33] "Peugeot"       "Volvo"         "CX Automotive" "GMC"          
## [37] "Jeep"         
## 
## $`1990`
##  [1] "Yugo"                     "Volvo"                   
##  [3] "Ford"                     "Alfa Romeo"              
##  [5] "Chrysler"                 "Consulier Industries Inc"
##  [7] "Honda"                    "Lotus"                   
##  [9] "Mazda"                    "Nissan"                  
## [11] "Porsche"                  "Toyota"                  
## [13] "Volkswagen"               "Acura"                   
## [15] "Audi"                     "BMW"                     
## [17] "Daihatsu"                 "Dodge"                   
## [19] "Eagle"                    "Geo"                     
## [21] "Hyundai"                  "Mitsubishi"              
## [23] "Isuzu"                    "Plymouth"                
## [25] "Pontiac"                  "Saab"                    
## [27] "Subaru"                   "Suzuki"                  
## [29] "Buick"                    "Chevrolet"               
## [31] "Mercury"                  "Oldsmobile"              
## [33] "Peugeot"                  "GMC"                     
## [35] "Jeep"                    
## 
## $`1991`
##  [1] "Import Trade Services"    "Alfa Romeo"              
##  [3] "Consulier Industries Inc" "Honda"                   
##  [5] "Lotus"                    "Mazda"                   
##  [7] "Toyota"                   "Nissan"                  
##  [9] "Mercury"                  "Porsche"                 
## [11] "Volkswagen"               "Acura"                   
## [13] "BMW"                      "Chevrolet"               
## [15] "Chrysler"                 "Daihatsu"                
## [17] "Dodge"                    "Eagle"                   
## [19] "Ford"                     "Geo"                     
## [21] "Hyundai"                  "Isuzu"                   
## [23] "Mercedes-Benz"            "Mitsubishi"              
## [25] "Plymouth"                 "Pontiac"                 
## [27] "Saab"                     "Saturn"                  
## [29] "Subaru"                   "Suzuki"                  
## [31] "Wallace Environmental"    "Buick"                   
## [33] "Infiniti"                 "Oldsmobile"              
## [35] "Peugeot"                  "Volvo"                   
## [37] "GMC"                      "Jeep"                    
## 
## $`1992`
##  [1] "Acura"                    "BMW"                     
##  [3] "Wallace Environmental"    "Peugeot"                 
##  [5] "Alfa Romeo"               "Consulier Industries Inc"
##  [7] "Lotus"                    "Mazda"                   
##  [9] "Porsche"                  "Toyota"                  
## [11] "Nissan"                   "Mercury"                 
## [13] "Volkswagen"               "Chevrolet"               
## [15] "Chrysler"                 "Daihatsu"                
## [17] "Dodge"                    "Eagle"                   
## [19] "Ford"                     "Geo"                     
## [21] "Honda"                    "Hyundai"                 
## [23] "Isuzu"                    "Mercedes-Benz"           
## [25] "Mitsubishi"               "Plymouth"                
## [27] "Pontiac"                  "Saab"                    
## [29] "Saturn"                   "Suzuki"                  
## [31] "Buick"                    "Infiniti"                
## [33] "Oldsmobile"               "Subaru"                  
## [35] "Volvo"                    "GMC"                     
## [37] "Jeep"                    
## 
## $`1993`
##  [1] "Subaru"        "Toyota"        "Volkswagen"    "Volvo"        
##  [5] "Buick"         "Chevrolet"     "Dodge"         "Hyundai"      
##  [9] "Mazda"         "Oldsmobile"    "Plymouth"      "Saab"         
## [13] "Ford"          "Honda"         "Mercury"       "Saturn"       
## [17] "Eagle"         "Mitsubishi"    "Nissan"        "GMC"          
## [21] "Isuzu"         "Jeep"          "Geo"           "Suzuki"       
## [25] "Infiniti"      "J.K. Motors"   "Kia"           "Alfa Romeo"   
## [29] "Lotus"         "Porsche"       "Acura"         "BMW"          
## [33] "Chrysler"      "Mercedes-Benz" "Pontiac"      
## 
## $`1994`
##  [1] "Alfa Romeo"    "Honda"         "Lotus"         "Mazda"        
##  [5] "Porsche"       "Toyota"        "BMW"           "Mercury"      
##  [9] "Acura"         "Chevrolet"     "Nissan"        "Dodge"        
## [13] "Eagle"         "Ford"          "Hyundai"       "Mitsubishi"   
## [17] "Plymouth"      "Pontiac"       "Saab"          "Saturn"       
## [21] "Subaru"        "Suzuki"        "Buick"         "Chrysler"     
## [25] "Geo"           "Kia"           "Infiniti"      "Mercedes-Benz"
## [29] "Oldsmobile"    "Volkswagen"    "Volvo"         "GMC"          
## [33] "Isuzu"         "Jeep"         
## 
## $`1995`
##  [1] "Honda"         "Lotus"         "Mazda"         "Porsche"      
##  [5] "Toyota"        "BMW"           "Nissan"        "Acura"        
##  [9] "Eagle"         "Ford"          "Geo"           "Hyundai"      
## [13] "Mitsubishi"    "Saab"          "Saturn"        "Subaru"       
## [17] "Volkswagen"    "Buick"         "Chevrolet"     "Chrysler"     
## [21] "Dodge"         "Kia"           "Infiniti"      "Mercury"      
## [25] "Mercedes-Benz" "Oldsmobile"    "Plymouth"      "Pontiac"      
## [29] "Volvo"         "GMC"           "Isuzu"         "Jeep"         
## [33] "Suzuki"       
## 
## $`1996`
##  [1] "BMW"           "Honda"         "Mazda"         "Suzuki"       
##  [5] "Nissan"        "Mitsubishi"    "Toyota"        "Acura"        
##  [9] "Eagle"         "Ford"          "Geo"           "Saturn"       
## [13] "Subaru"        "Volkswagen"    "Buick"         "Chevrolet"    
## [17] "Chrysler"      "Dodge"         "Kia"           "Hyundai"      
## [21] "Infiniti"      "Mercury"       "Mercedes-Benz" "Oldsmobile"   
## [25] "Plymouth"      "Pontiac"       "Isuzu"         "GMC"          
## [29] "Jeep"          "Saab"         
## 
## $`1997`
##  [1] "BMW"           "Honda"         "Mazda"         "Suzuki"       
##  [5] "Nissan"        "Mitsubishi"    "Toyota"        "Acura"        
##  [9] "Eagle"         "Ford"          "Geo"           "Hyundai"      
## [13] "Saab"          "Saturn"        "Subaru"        "Volkswagen"   
## [17] "Audi"          "Buick"         "Chevrolet"     "Chrysler"     
## [21] "Dodge"         "Kia"           "Mercury"       "Mercedes-Benz"
## [25] "Oldsmobile"    "Plymouth"      "Pontiac"       "Isuzu"        
## [29] "GMC"           "Jeep"         
## 
## $`1998`
##  [1] "BMW"                  "Suzuki"               "Mitsubishi"          
##  [4] "Nissan"               "Toyota"               "Acura"               
##  [7] "Chevrolet"            "Eagle"                "Ford"                
## [10] "Honda"                "Hyundai"              "Pontiac"             
## [13] "Saab"                 "Saturn"               "Subaru"              
## [16] "Volkswagen"           "Audi"                 "Buick"               
## [19] "Chrysler"             "Daewoo"               "Dodge"               
## [22] "Kia"                  "Mercury"              "Mazda"               
## [25] "Mercedes-Benz"        "Oldsmobile"           "Plymouth"            
## [28] "Isuzu"                "GMC"                  "Jeep"                
## [31] "Quantum Technologies"
## 
## $`1999`
##  [1] "Mazda"                "Mercedes-Benz"        "Mitsubishi"          
##  [4] "Toyota"               "Acura"                "Chevrolet"           
##  [7] "Ford"                 "Honda"                "Hyundai"             
## [10] "Nissan"               "Pontiac"              "Saab"                
## [13] "Saturn"               "Subaru"               "Suzuki"              
## [16] "Volkswagen"           "Audi"                 "BMW"                 
## [19] "Chrysler"             "Daewoo"               "Dodge"               
## [22] "Infiniti"             "Kia"                  "Mercury"             
## [25] "Oldsmobile"           "Plymouth"             "Isuzu"               
## [28] "GMC"                  "Jeep"                 "Quantum Technologies"
## 
## $`2000`
##  [1] "Honda"         "Mazda"         "Mercedes-Benz" "Toyota"       
##  [5] "Audi"          "Acura"         "Chevrolet"     "Ford"         
##  [9] "Hyundai"       "Mitsubishi"    "Pontiac"       "Saab"         
## [13] "Saturn"        "Subaru"        "Suzuki"        "Volkswagen"   
## [17] "Daewoo"        "Dodge"         "Infiniti"      "Kia"          
## [21] "Mercury"       "Nissan"        "Oldsmobile"    "Plymouth"     
## [25] "Volvo"         "Chrysler"      "GMC"           "Isuzu"        
## [29] "Jeep"         
## 
## $`2001`
##  [1] "Audi"          "Honda"         "Mazda"         "Mercedes-Benz"
##  [5] "Toyota"        "Mitsubishi"    "Acura"         "Chevrolet"    
##  [9] "Ford"          "Hyundai"       "Saab"          "Saturn"       
## [13] "Subaru"        "Suzuki"        "Volkswagen"    "Chrysler"     
## [17] "Daewoo"        "Dodge"         "Infiniti"      "Kia"          
## [21] "Mercury"       "Nissan"        "Oldsmobile"    "Plymouth"     
## [25] "Pontiac"       "Volvo"         "Isuzu"         "Jeep"         
## 
## $`2002`
##  [1] "Audi"          "Honda"         "Mazda"         "Mercedes-Benz"
##  [5] "Toyota"        "MINI"          "Mitsubishi"    "Acura"        
##  [9] "Ford"          "Hyundai"       "Saab"          "Saturn"       
## [13] "Subaru"        "Suzuki"        "Volkswagen"    "Chevrolet"    
## [17] "Chrysler"      "Daewoo"        "Dodge"         "Infiniti"     
## [21] "Kia"           "Mercury"       "Nissan"        "Oldsmobile"   
## [25] "Pontiac"       "Volvo"         "Isuzu"         "Jeep"         
## 
## $`2003`
##  [1] "Audi"          "Honda"         "Mazda"         "Mercedes-Benz"
##  [5] "Toyota"        "Mitsubishi"    "Volkswagen"    "Acura"        
##  [9] "Ford"          "Hyundai"       "Saab"          "Subaru"       
## [13] "Chevrolet"     "Chrysler"      "Daewoo"        "Dodge"        
## [17] "Kia"           "Nissan"        "Oldsmobile"    "Pontiac"      
## [21] "Saturn"        "Suzuki"        "Volvo"         "GMC"          
## [25] "Isuzu"         "Jeep"          "MINI"         
## 
## $`2004`
##  [1] "Audi"          "Honda"         "Mazda"         "Mercedes-Benz"
##  [5] "Toyota"        "MINI"          "Mitsubishi"    "Volkswagen"   
##  [9] "Acura"         "Hyundai"       "Saab"          "Subaru"       
## [13] "Scion"         "Chevrolet"     "Chrysler"      "Daewoo"       
## [17] "Dodge"         "Ford"          "Kia"           "Nissan"       
## [21] "Oldsmobile"    "Pontiac"       "Saturn"        "Suzuki"       
## [25] "Volvo"         "GMC"           "Jeep"         
## 
## $`2005`
##  [1] "Audi"          "Honda"         "Lotus"         "Mazda"        
##  [5] "Toyota"        "MINI"          "Mitsubishi"    "Volkswagen"   
##  [9] "Acura"         "Chevrolet"     "Hyundai"       "Saab"         
## [13] "Subaru"        "Scion"         "Chrysler"      "Dodge"        
## [17] "Ford"          "Kia"           "Mercedes-Benz" "Nissan"       
## [21] "Pontiac"       "Saturn"        "Suzuki"        "GMC"          
## [25] "Jeep"          "Mercury"      
## 
## $`2006`
##  [1] "Audi"       "Honda"      "Mazda"      "Pontiac"    "MINI"      
##  [6] "Acura"      "Chevrolet"  "Hyundai"    "Mitsubishi" "Saab"      
## [11] "Subaru"     "Suzuki"     "Scion"      "Chrysler"   "Ford"      
## [16] "Kia"        "Nissan"     "Saturn"     "Toyota"     "Volkswagen"
## [21] "Dodge"      "Mercury"    "GMC"        "Isuzu"      "Jeep"      
## [26] "Lotus"     
## 
## $`2007`
##  [1] "Mitsubishi" "Chevrolet"  "Pontiac"    "Subaru"     "Toyota"    
##  [6] "Ford"       "Hyundai"    "Saturn"     "Volkswagen" "Honda"     
## [11] "Mazda"      "Mercury"    "Lotus"      "Audi"       "Scion"     
## [16] "Suzuki"     "Nissan"     "GMC"        "Isuzu"      "Saab"      
## [21] "Acura"      "Chrysler"   "Dodge"      "Kia"        "Jeep"      
## [26] "MINI"      
## 
## $`2008`
##  [1] "Audi"       "Mitsubishi" "Chrysler"   "Scion"      "Volkswagen"
##  [6] "Subaru"     "Dodge"      "Hyundai"    "Ford"       "Mazda"     
## [11] "Mercury"    "Saturn"     "Pontiac"    "Chevrolet"  "Acura"     
## [16] "Suzuki"     "Toyota"     "Kia"        "Nissan"     "GMC"       
## [21] "Isuzu"      "Honda"      "Lotus"      "MINI"       "Saab"      
## [26] "Jeep"      
## 
## $`2009`
##  [1] "Audi"       "Mitsubishi" "Chevrolet"  "Pontiac"    "Scion"     
##  [6] "Acura"      "Subaru"     "Toyota"     "Volkswagen" "Kia"       
## [11] "Hyundai"    "Mazda"      "Dodge"      "Saturn"     "MINI"      
## [16] "Ford"       "Mercury"    "Nissan"     "GMC"        "Honda"     
## [21] "Chrysler"   "Suzuki"     "Jeep"       "Saab"       "Lotus"     
## 
## $`2010`
##  [1] "Pontiac"    "Saturn"     "Hyundai"    "Scion"      "Kia"       
##  [6] "Mazda"      "Toyota"     "Ford"       "Mercury"    "Chevrolet" 
## [11] "GMC"        "Subaru"     "Audi"       "Mitsubishi" "Volkswagen"
## [16] "Acura"      "Suzuki"     "Dodge"      "Jeep"       "Saab"      
## [21] "Chrysler"   "Nissan"     "Honda"      "Lotus"      "MINI"      
## [26] "Buick"     
## 
## $`2011`
##  [1] "Mitsubishi" "Ford"       "Kia"        "Subaru"     "Buick"     
##  [6] "Toyota"     "Hyundai"    "Scion"      "Chevrolet"  "GMC"       
## [11] "Audi"       "Mazda"      "Volkswagen" "Suzuki"     "Mercury"   
## [16] "Nissan"     "Acura"      "MINI"       "Honda"      "Dodge"     
## [21] "Jeep"       "Chrysler"   "Saab"       "Lotus"     
## 
## $`2012`
##  [1] "Fiat"          "Mitsubishi"    "Audi"          "Volkswagen"   
##  [5] "Ford"          "Honda"         "Hyundai"       "Saab"         
##  [9] "Mazda"         "Scion"         "Kia"           "Chevrolet"    
## [13] "GMC"           "Nissan"        "Chrysler"      "Dodge"        
## [17] "MINI"          "Acura"         "Mercedes-Benz" "Subaru"       
## [21] "Suzuki"        "Toyota"        "Buick"         "Jeep"         
## [25] "BMW"           "Land Rover"   
## 
## $`2013`
##  [1] "Volkswagen"    "Mazda"         "Hyundai"       "Ford"         
##  [5] "Kia"           "Scion"         "Subaru"        "BMW"          
##  [9] "Dodge"         "Audi"          "Lincoln"       "Chevrolet"    
## [13] "Toyota"        "Mercedes-Benz" "Nissan"        "GMC"          
## [17] "Buick"         "Cadillac"      "Jeep"          "MINI"         
## [21] "Chrysler"      "Mitsubishi"    "Fiat"          "Suzuki"       
## [25] "Acura"         "Honda"         "Land Rover"    "Jaguar"       
## 
## $`2014`
##  [1] "Mazda"         "Subaru"        "Kia"           "Jeep"         
##  [5] "BMW"           "Acura"         "Mitsubishi"    "Nissan"       
##  [9] "Chevrolet"     "Lincoln"       "Ford"          "Audi"         
## [13] "Scion"         "Buick"         "Cadillac"      "Volkswagen"   
## [17] "GMC"           "Toyota"        "Fiat"          "MINI"         
## [21] "Mercedes-Benz" "Jaguar"        "Dodge"         "Chrysler"     
## [25] "Honda"         "Hyundai"       "Land Rover"   
## 
## $`2015`
##  [1] "Mazda"         "Kia"           "Audi"          "Subaru"       
##  [5] "BMW"           "MINI"          "Volkswagen"    "Volvo"        
##  [9] "Chrysler"      "Honda"         "Nissan"        "Chevrolet"    
## [13] "Land Rover"    "Scion"         "Lincoln"       "Buick"        
## [17] "Hyundai"       "Jaguar"        "Ford"          "GMC"          
## [21] "Cadillac"      "Fiat"          "Acura"         "Mitsubishi"   
## [25] "Mercedes-Benz" "Toyota"        "Dodge"         "Jeep"         
## [29] "Alfa Romeo"    "Ram"           "Lexus"        
## 
## attr(,"split_type")
## [1] "data.frame"
## attr(,"split_labels")
##    year
## 1  1984
## 2  1985
## 3  1986
## 4  1987
## 5  1988
## 6  1989
## 7  1990
## 8  1991
## 9  1992
## 10 1993
## 11 1994
## 12 1995
## 13 1996
## 14 1997
## 15 1998
## 16 1999
## 17 2000
## 18 2001
## 19 2002
## 20 2003
## 21 2004
## 22 2005
## 23 2006
## 24 2007
## 25 2008
## 26 2009
## 27 2010
## 28 2011
## 29 2012
## 30 2013
## 31 2014
## 32 2015
commonMakes <- Reduce(intersect, uniqMakes)
commonMakes
##  [1] "Ford"       "Honda"      "Toyota"     "Volkswagen" "Chevrolet" 
##  [6] "Chrysler"   "Nissan"     "Dodge"      "Mazda"      "Mitsubishi"
## [11] "Subaru"     "Jeep"
# there is reduce concept here, I need to spend more time to practice this one.

carsCommonMakes4 <- subset(gasCars4, make %in% commonMakes)
avgMPG_commonMakes <- ddply(carsCommonMakes4, ~year + make, summarise, avgMPG = mean(comb08))
ggplot(avgMPG_commonMakes, aes(year, avgMPG)) + geom_line() + facet_wrap(~make, nrow = 3) + ggtitle("Fig 9: MPG of Makes over the Year")