Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the group_by() function do in R's dplyr package?
easy
A. It splits data into groups based on specified columns.
B. It sorts the data in ascending order.
C. It removes duplicate rows from the data.
D. It changes the data type of columns.

Solution

  1. Step 1: Understand the purpose of group_by()

    The group_by() function is used to split data into groups based on one or more columns.
  2. Step 2: Compare with other options

    Sorting, removing duplicates, or changing data types are done by other functions, not group_by().
  3. Final Answer:

    It splits data into groups based on specified columns. -> Option A
  4. Quick Check:

    group_by() = splits data into groups [OK]
Hint: Remember: group_by() groups data by columns [OK]
Common Mistakes:
  • Confusing group_by() with arrange() for sorting
  • Thinking group_by() removes duplicates
  • Assuming group_by() changes data types
2. Which of the following is the correct syntax to group a data frame df by the column category and then summarise the mean of value?
easy
A. df %>% group_by(category) %>% summarise(mean_value = mean(value))
B. df %>% summarise(mean_value = mean(value)) %>% group_by(category)
C. group_by(df, category) %>% summarise(mean_value = mean(value))
D. df %>% group_by(category) %>% mean(value)

Solution

  1. Step 1: Identify correct pipe sequence

    The correct order is to first group the data, then summarise it. So df %>% group_by(category) %>% summarise(...) is correct.
  2. Step 2: Check function usage

    mean(value) inside summarise is correct. df %>% summarise(mean_value = mean(value)) %>% group_by(category) reverses the order, C uses wrong syntax, and D misses summarise.
  3. Final Answer:

    df %>% group_by(category) %>% summarise(mean_value = mean(value)) -> Option A
  4. Quick Check:

    group_by() then summarise() = df %>% group_by(category) %>% summarise(mean_value = mean(value)) [OK]
Hint: Always group before summarising with %>% [OK]
Common Mistakes:
  • Putting summarise() before group_by()
  • Using mean() outside summarise()
  • Incorrect function chaining syntax
3. Given the data frame df below:
df <- data.frame(group = c('A', 'A', 'B', 'B', 'B'), value = c(10, 20, 30, 40, 50))

What is the output of:
df %>% group_by(group) %>% summarise(total = sum(value))
medium
A. group | total -----|------ A | 10 B | 30
B. group | total -----|------ A | 30 B | 90
C. group | total -----|------ A | 20 B | 50
D. group | total -----|------ A | 30 B | 120

Solution

  1. Step 1: Group data by 'group'

    There are two groups: 'A' with values 10 and 20, and 'B' with values 30, 40, 50.
  2. Step 2: Calculate sum of 'value' for each group

    Sum for 'A' = 10 + 20 = 30; Sum for 'B' = 30 + 40 + 50 = 120.
  3. Final Answer:

    group | total -----|------ A | 30 B | 120 -> Option D
  4. Quick Check:

    sum values by group = group | total -----|------ A | 30 B | 120 [OK]
Hint: Add values per group to find sum quickly [OK]
Common Mistakes:
  • Adding only first two values of group B
  • Mixing group labels and values
  • Confusing sum with mean
4. Consider this code snippet:
df <- data.frame(team = c('X', 'X', 'Y'), score = c(5, 7, 8))
df %>% summarise(avg_score = mean(score)) %>% group_by(team)

What is the main issue here?
medium
A. The code will produce an error because group_by() must come first.
B. The summarise() is used before group_by(), so grouping is lost.
C. The mean() function is used incorrectly inside summarise().
D. The data frame df is missing required columns.

Solution

  1. Step 1: Analyze function order

    The code summarises the whole data first, losing group info, then tries to group after summarising.
  2. Step 2: Understand grouping effect

    Grouping after summarise has no effect because summarise returns a smaller data frame without original groups.
  3. Final Answer:

    The summarise() is used before group_by(), so grouping is lost. -> Option B
  4. Quick Check:

    group_by() must come before summarise() [OK]
Hint: Always group before summarising to keep groups [OK]
Common Mistakes:
  • Assuming group_by() after summarise() works
  • Thinking summarise() keeps original groups
  • Believing mean() usage is wrong here
5. You have a data frame sales with columns region, product, and revenue. You want to find the average revenue per product only for regions where total revenue exceeds 1000. Which code correctly achieves this?
hard
A. sales %>% group_by(region) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% group_by(product) %>% summarise(avg_rev = mean(revenue))
B. sales %>% group_by(region, product) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% summarise(avg_rev = mean(total_rev))
C. sales %>% group_by(region) %>% filter(sum(revenue) > 1000) %>% group_by(product) %>% summarise(avg_rev = mean(revenue))
D. sales %>% group_by(product) %>% summarise(avg_rev = mean(revenue)) %>% filter(region %in% (sales %>% group_by(region) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% pull(region)))

Solution

  1. Step 1: Filter regions with total revenue > 1000

    Using filter(sum(revenue) > 1000) inside group_by(region) keeps only those regions.
  2. Step 2: Group by product and summarise average revenue

    After filtering, regroup by product and calculate mean revenue per product.
  3. Step 3: Check other options

    sales %>% group_by(region) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% group_by(product) %>% summarise(avg_rev = mean(revenue)) tries to summarise twice but loses revenue values for product grouping. sales %>% group_by(region, product) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% summarise(avg_rev = mean(total_rev)) mixes grouping and summarising incorrectly. sales %>% group_by(product) %>% summarise(avg_rev = mean(revenue)) %>% filter(region %in% (sales %>% group_by(region) %>% summarise(total_rev = sum(revenue)) %>% filter(total_rev > 1000) %>% pull(region))) filters after summarising by product, which is wrong.
  4. Final Answer:

    sales %>% group_by(region) %>% filter(sum(revenue) > 1000) %>% group_by(product) %>% summarise(avg_rev = mean(revenue)) -> Option C
  5. Quick Check:

    Filter groups before summarise for correct subset [OK]
Hint: Filter groups with filter(sum(...) > val) before summarise [OK]
Common Mistakes:
  • Summarising before filtering groups
  • Losing original revenue values after summarise
  • Incorrect chaining of group_by and summarise
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard