Complete the code to group the data by 'cyl' column.
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by([1])We use group_by(cyl) to group the data by the number of cylinders.
Complete the code to calculate the average miles per gallon (mpg) for each group.
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by(cyl) %>% summarise(avg_mpg = [1](mpg))The mean() function calculates the average mpg for each cylinder group.
Fix the error in the summarise() function to count the number of cars in each group.
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by(cyl) %>% summarise(count = [1]())The n() function counts the number of rows in each group inside summarise().
Fill both blanks to calculate the average horsepower (hp) and maximum weight (wt) for each group.
library(dplyr) data <- mtcars grouped_data <- data %>% group_by(cyl) %>% summarise(avg_hp = [1](hp), max_wt = [2](wt))
Use mean() to get average horsepower and max() to get maximum weight per group.
Fill all three blanks to group by 'gear', then calculate the count, average mpg, and minimum quarter mile time (qsec).
library(dplyr) data <- mtcars grouped_data <- data %>% group_by([1]) %>% summarise(count = [2](), avg_mpg = [3](mpg), min_qsec = min(qsec))
Group by gear, count rows with n(), and average mpg with mean().