0
0
R Programmingprogramming~10 mins

summarise() with group_by() in R Programming - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to group the data by 'cyl' column.

R Programming
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by([1])
Drag options to blanks, or click blank then click option'
Acyl
Bmpg
Chp
Dgear
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column that is not categorical for grouping.
Forgetting to load dplyr library.
2fill in blank
medium

Complete the code to calculate the average miles per gallon (mpg) for each group.

R Programming
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by(cyl) %>% summarise(avg_mpg = [1](mpg))
Drag options to blanks, or click blank then click option'
Asum
Bmean
Cmedian
Dmax
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum instead of mean, which gives total not average.
Using max or median which are different statistics.
3fill in blank
hard

Fix the error in the summarise() function to count the number of cars in each group.

R Programming
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by(cyl) %>% summarise(count = [1]())
Drag options to blanks, or click blank then click option'
Asum
Blength
Ccount
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which does not work inside summarise for grouped data.
Using count() which is not a function inside summarise.
4fill in blank
hard

Fill both blanks to calculate the average horsepower (hp) and maximum weight (wt) for each group.

R Programming
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by(cyl) %>% summarise(avg_hp = [1](hp), max_wt = [2](wt))
Drag options to blanks, or click blank then click option'
Amean
Bmax
Cmin
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up min and max functions.
Using sum instead of mean for average.
5fill in blank
hard

Fill all three blanks to group by 'gear', then calculate the count, average mpg, and minimum quarter mile time (qsec).

R Programming
library(dplyr)
data <- mtcars
grouped_data <- data %>% group_by([1]) %>% summarise(count = [2](), avg_mpg = [3](mpg), min_qsec = min(qsec))
Drag options to blanks, or click blank then click option'
Agear
Bn
Cmean
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong column for grouping.
Using sum() instead of n() for counting.