Homework 2: Manipulating data frames (Answers)

Corey Jackson

2020-01-20 12:05:16

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),]

# 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)]