In: Computer Science
Using R Studio: 1)Use the `mtcars` data (`data(mtcars)`) to answer these questions: a) Which rows of the data frame contain cars that weigh more than 4000 pounds (the variable is `wt`, units are 1000 pounds). b) Which cars are these? (*Hint:* since rows are named by car name, use `row.names()`). c) What is the mean displacement (in inches^3^ ) for cars with at least 200 horsepower (`hp`). d) Which car has the highest fuel economy (`mpg`)? e) What was the fuel economy for the Honda Civic? 2) Using the `mtcars` data create a new variable for horespower per unit weight (`hp/wt`). Is this a better predictor of acceleration (`qsec`; seconds to complete a quarter mile) than horsepower alone? 3) Use the function `subset()` to return the cars with 4 cyclinders and automatic transmissions (`am` = `0`). (*Hint:* see `?subset`).
Question 1)
Below is the R code for the given problem. Please follow comments-
R Code-
# Question 1
# a)
# filtering cars with wt more than 4
filter(mtcars, wt > 4.0)
# b)
# List of cars with wt more than 4
mtcars[ which(mtcars$wt>4 ), ]
# c)
# Finding mean disp of cars with hp greater than 200
mtcars %>%
filter(hp > 200) %>%
summarise(mean = mean(disp))
# d)
# Using max function to find cars with maximum mpg
mtcars[ which(mtcars$mpg== max(mtcars$mpg)),]
# e)
# Fuel economy of honda civic
mtcars[grep('^Honda', rownames(mtcars)),]
Code screenshot-
Output-