How does aggregate() work in base R and how does it relate to split-apply-combine?

Quick Overview

Asks how the base R aggregate() function groups rows, applies a summary function and returns a data frame, and how that maps to split-apply-combine. It tests the formula and by-list interfaces, missing value handling and when to reach for dplyr or tapply.

How does aggregate() work in base R and how does it relate to split-apply-combine?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

How does `aggregate()` work in base R and how does it relate to split-apply-combine? ```hint Map the three steps Match each of split, apply and combine to an argument or to the output of the function, then compare its two calling styles. ``` ### What a Strong Answer Covers - What `aggregate()` returns: a data frame with one row per group. - How split, apply and combine map to the grouping variables, the `FUN` argument and the stacked result. - The formula and the `by`-list calling styles, and how the output columns get their names. - The limits (what `FUN` may return, how missing values are handled) and when another tool such as `tapply()`, `split()` with `lapply()`, or dplyr fits better. ### Follow-up Questions - How do the formula and `by`-list methods treat missing values, and how would you keep rows where only the value column is NA? - What happens when `FUN` returns two numbers per group, and how do you get ordinary columns back? - How would you write the same aggregation by hand with `split()` and `sapply()`, and with dplyr? - How would you summarize several columns at once, with a different function for each?

Overview: Asks how the base R aggregate() function groups rows, applies a summary function and returns a data frame, and how that maps to split-apply-combine. It tests the formula and by-list interfaces, missing value handling and when to reach for dplyr or tapply.

Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

How does aggregate() work in base R and how does it relate to split-apply-combine?

What a Strong Answer Covers Guidance

  • What aggregate() returns: a data frame with one row per group.
  • How split, apply and combine map to the grouping variables, the FUN argument and the stacked result.
  • The formula and the by -list calling styles, and how the output columns get their names.
  • The limits (what FUN may return, how missing values are handled) and when another tool such as tapply() , split() with lapply() , or dplyr fits better.

Follow-up Questions Guidance

  • How do the formula and by -list methods treat missing values, and how would you keep rows where only the value column is NA?
  • What happens when FUN returns two numbers per group, and how do you get ordinary columns back?
  • How would you write the same aggregation by hand with split() and sapply() , and with dplyr?
  • How would you summarize several columns at once, with a different function for each?