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
intermediate2: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)Attempts:
2 left
💡 Hint
Think about how group_by() splits data and summarise() calculates one value per group.
✗ Incorrect
The code groups the mtcars dataset by the number of cylinders (cyl) and calculates the average miles per gallon (mpg) for each group. The result is a tibble with 3 rows, one for each unique cyl value, and two columns: cyl and avg_mpg.
❓ Predict Output
intermediate2: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)Attempts:
2 left
💡 Hint
summarise() without group_by() returns one summary row for the whole dataset.
✗ Incorrect
Without grouping, summarise() calculates the mean horsepower (hp) for all rows combined, returning a single-row tibble with one column avg_hp.
🧠 Conceptual
advanced2: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)
Attempts:
2 left
💡 Hint
summarise() can create multiple summary columns at once.
✗ Incorrect
The code groups mtcars by cyl and calculates two summaries per group: average mpg and maximum hp. The output includes the grouping column cyl plus the two summaries.
🔧 Debug
advanced2: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)Attempts:
2 left
💡 Hint
Check if the column name used in mean() exists in mtcars.
✗ Incorrect
The mtcars dataset does not have a column named 'miles_per_gallon'. Using a non-existent column in summarise() causes an error.
🚀 Application
expert3:00remaining
Count unique gear values per cylinder group
Which code correctly counts the number of unique gear values for each cylinder group in mtcars?
Attempts:
2 left
💡 Hint
Use group_by() first, then summarise() with n_distinct() to count unique values per group.
✗ Incorrect
Option C groups by cyl and then counts distinct gear values per group correctly. Option C is close but length(unique()) inside summarise() works but n_distinct() is preferred and more efficient. Option C groups after summarise, which is incorrect. Option C groups by gear, not cyl.