0
0
R Programmingprogramming~20 mins

summarise() with group_by() in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of summarise() with group_by()
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of summarise() with group_by() on mtcars
What is the output of this R code snippet?
R Programming
library(dplyr)
result <- mtcars %>% group_by(cyl) %>% summarise(avg_mpg = mean(mpg))
print(result)
A
A tibble: 3 × 2
    cyl avg_mpg
  &lt;dbl&gt;   &lt;dbl&gt;
1     4    26.7
2     6    19.7
3     8    15.1
B
A tibble: 32 × 2
    cyl avg_mpg
  &lt;dbl&gt;   &lt;dbl&gt;
1     6    20.1
2     4    22.8
3     8    15.0
... (repeated for each row)
C
A tibble: 3 × 1
    avg_mpg
    &lt;dbl&gt;
1    20.1
2    22.8
3    15.0
DError in summarise(): object 'mpg' not found
Attempts:
2 left
💡 Hint
Think about how group_by() splits data and summarise() calculates one value per group.
Predict Output
intermediate
2:00remaining
Result of summarise() without group_by()
What will this R code output?
R Programming
library(dplyr)
result <- mtcars %>% summarise(avg_hp = mean(hp))
print(result)
A
A tibble: 32 × 1
  avg_hp
   &lt;dbl&gt;
1   110
2   110
... (repeated for each row)
B
A tibble: 1 × 1
  avg_hp
   &lt;dbl&gt;
1   146.7
CError: summarise() requires grouping variables
D
A tibble: 1 × 2
  cyl avg_hp
  &lt;dbl&gt;  &lt;dbl&gt;
1    NA   146.7
Attempts:
2 left
💡 Hint
summarise() without group_by() returns one summary row for the whole dataset.
🧠 Conceptual
advanced
2:30remaining
Understanding summarise() with multiple summaries
What does this code produce?
R Programming
library(dplyr)
result <- mtcars %>% group_by(cyl) %>% summarise(avg_mpg = mean(mpg), max_hp = max(hp))
print(result)
AA tibble with 32 rows, each showing avg_mpg and max_hp repeated for each car
BA tibble with 3 rows and 2 columns: avg_mpg and max_hp only, no cyl column
CError: summarise() can only return one summary column
DA tibble with 3 rows and 3 columns: cyl, avg_mpg, max_hp showing average mpg and max hp per cylinder group
Attempts:
2 left
💡 Hint
summarise() can create multiple summary columns at once.
🔧 Debug
advanced
2:00remaining
Identify the error in summarise() with group_by()
What error does this code produce?
R Programming
library(dplyr)
result <- mtcars %>% group_by(cyl) %>% summarise(avg_mpg = mean(miles_per_gallon))
print(result)
AError: object 'miles_per_gallon' not found
BError: group_by() requires a grouping variable
CNo error, outputs average mpg per cyl
DError: summarise() cannot be used after group_by()
Attempts:
2 left
💡 Hint
Check if the column name used in mean() exists in mtcars.
🚀 Application
expert
3:00remaining
Count unique gear values per cylinder group
Which code correctly counts the number of unique gear values for each cylinder group in mtcars?
Amtcars %>% summarise(unique_gears = n_distinct(gear)) %>% group_by(cyl)
Bmtcars %>% group_by(cyl) %>% summarise(unique_gears = length(unique(gear)))
Cmtcars %>% group_by(cyl) %>% summarise(unique_gears = n_distinct(gear))
Dmtcars %>% group_by(gear) %>% summarise(unique_cyl = n_distinct(cyl))
Attempts:
2 left
💡 Hint
Use group_by() first, then summarise() with n_distinct() to count unique values per group.