Complete the code to select the column 'mpg' from the mtcars dataset.
library(dplyr) result <- mtcars %>% select([1]) print(result)
The select() function chooses columns by name. Here, 'mpg' is the correct column to select.
Complete the code to filter rows where 'cyl' equals 6.
library(dplyr) result <- mtcars %>% filter([1] == 6) print(result)
The filter() function keeps rows where the condition is true. Here, we want rows with 6 cylinders, so we use 'cyl'.
Fix the error in the code to arrange the dataset by 'hp' in descending order.
library(dplyr) result <- mtcars %>% arrange([1](hp)) print(result)
The arrange() function orders rows. To sort descending, use desc() around the column name.
Fill both blanks to create a new column 'power_to_weight' as hp divided by wt.
library(dplyr) result <- mtcars %>% mutate(power_to_weight = [1] / [2]) print(result$power_to_weight)
The mutate() function adds new columns. Here, 'power_to_weight' is horsepower divided by weight ('wt').
Fill both blanks to create a summary with average mpg for cars with more than 4 gears.
library(dplyr) summary <- mtcars %>% filter([1] > 4) %>% summarise(avg_mpg = mean([2])) print(summary)
First, filter cars with more than 4 gears using 'gear'. Then calculate the average mpg with mean(mpg).