Explore the mtcars dataset (which is already included in R). Copy the mtcars dataset into a new variable (called it myCars), so that if you mess up, you can start again very easily (by copying mtcars into myCars again).

### Step 1: What is the hp (hp stands for “horse power”)

myCars <- mtcars

#1) What is the highest hp?
max(myCars$hp) ## [1] 335 #2) Which car has the highest hp? myCars[myCars$hp == max(myCars$hp),] ## mpg cyl disp hp drat wt qsec vs am gear carb ## Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8 ### Step 2: Explore mpg (mpg stands for “miles per gallon”) #3) What is the highest mpg? max(myCars$mpg)
## [1] 33.9
#4) Which car has the highest mpg?
myCars[which.max(myCars$mpg),] ## mpg cyl disp hp drat wt qsec vs am gear carb ## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.9 1 1 4 1 # or which.max(myCars$mpg)
## [1] 20
myCars[20,]
##                 mpg cyl disp hp drat    wt qsec vs am gear carb
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
# or
maxmpgcar <- myCars[myCars$mpg == max(myCars$mpg),]
maxmpgcar
##                 mpg cyl disp hp drat    wt qsec vs am gear carb
## Toyota Corolla 33.9   4 71.1 65 4.22 1.835 19.9  1  1    4    1
#5) Create a sorted dataframe, based on mpg
orderedmpg <- myCars[order(myCars$mpg),] head(orderedmpg) ## mpg cyl disp hp drat wt qsec vs am gear carb ## Cadillac Fleetwood 10.4 8 472 205 2.93 5.250 17.98 0 0 3 4 ## Lincoln Continental 10.4 8 460 215 3.00 5.424 17.82 0 0 3 4 ## Camaro Z28 13.3 8 350 245 3.73 3.840 15.41 0 0 3 4 ## Duster 360 14.3 8 360 245 3.21 3.570 15.84 0 0 3 4 ## Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4 ## Maserati Bora 15.0 8 301 335 3.54 3.570 14.60 0 1 5 8 # or orderedmpgdesc <- myCars[order(-myCars$mpg),]
head(orderedmpgdesc)
##                 mpg cyl  disp  hp drat    wt  qsec vs am gear carb
## Toyota Corolla 33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
## Fiat 128       32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
## Honda Civic    30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
## Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
## Fiat X1-9      27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
## Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2

### Step 3: Which car has the “best” combination of mpg and hp?

1. What logic did you use?

You could visually inspect the data by ordering mpg and hp to determine which has the middle rank

1. Which car?

Lotus Europa

### Step 4: Which car has “best”" car combination of mpg and hp, where mpg and hp must be given equal weight?

myCars$bestpower <- myCars$hp/myCars$mpg myCars[which.max(myCars$bestpower),]
##               mpg cyl disp  hp drat   wt qsec vs am gear carb bestpower
## Maserati Bora  15   8  301 335 3.54 3.57 14.6  0  1    5    8  22.33333
#or
# scale 'mpg' by subtracting its column mean and then dividing the column’s standard deviation
scale(myCars$mpg) ## [,1] ## [1,] 0.15088482 ## [2,] 0.15088482 ## [3,] 0.44954345 ## [4,] 0.21725341 ## [5,] -0.23073453 ## [6,] -0.33028740 ## [7,] -0.96078893 ## [8,] 0.71501778 ## [9,] 0.44954345 ## [10,] -0.14777380 ## [11,] -0.38006384 ## [12,] -0.61235388 ## [13,] -0.46302456 ## [14,] -0.81145962 ## [15,] -1.60788262 ## [16,] -1.60788262 ## [17,] -0.89442035 ## [18,] 2.04238943 ## [19,] 1.71054652 ## [20,] 2.29127162 ## [21,] 0.23384555 ## [22,] -0.76168319 ## [23,] -0.81145962 ## [24,] -1.12671039 ## [25,] -0.14777380 ## [26,] 1.19619000 ## [27,] 0.98049211 ## [28,] 1.71054652 ## [29,] -0.71190675 ## [30,] -0.06481307 ## [31,] -0.84464392 ## [32,] 0.21725341 ## attr(,"scaled:center") ## [1] 20.09062 ## attr(,"scaled:scale") ## [1] 6.026948  # scale 'hp' by subtracting its column mean and then dividing its column’s standard deviation scale(myCars$hp)
##              [,1]
##  [1,] -0.53509284
##  [2,] -0.53509284
##  [3,] -0.78304046
##  [4,] -0.53509284
##  [5,]  0.41294217
##  [6,] -0.60801861
##  [7,]  1.43390296
##  [8,] -1.23518023
##  [9,] -0.75387015
## [10,] -0.34548584
## [11,] -0.34548584
## [12,]  0.48586794
## [13,]  0.48586794
## [14,]  0.48586794
## [15,]  0.85049680
## [16,]  0.99634834
## [17,]  1.21512565
## [18,] -1.17683962
## [19,] -1.38103178
## [20,] -1.19142477
## [21,] -0.72469984
## [22,]  0.04831332
## [23,]  0.04831332
## [24,]  1.43390296
## [25,]  0.41294217
## [26,] -1.17683962
## [27,] -0.81221077
## [28,] -0.49133738
## [29,]  1.71102089
## [30,]  0.41294217
## [31,]  2.74656682
## [32,] -0.54967799
## attr(,"scaled:center")
## [1] 146.6875
## attr(,"scaled:scale")
## [1] 68.56287
 # summarize the two scaled data and save the result as a new column 'combination' in the dataframe
myCars$combination <- scale(myCars$mpg) + scale(myCars$hp) # get the index of maximum combination first, and then get its row name row.names(myCars)[which.max(myCars$combination)]
## [1] "Maserati Bora"