Type-Safe Grouped Summaries With tapply and vapply

Quick Overview

Implement base R helpers that compute per-group means and summaries with tapply and type-checked column reductions with vapply. It tests control of group order, exact output types such as integer counts and named empty vectors, and why vapply templates are safer than sapply.

Type-Safe Grouped Summaries With tapply and vapply

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement base R helpers in `/app/solution.R` that compute grouped summaries with `tapply` and type-safe column reductions with `vapply`. Use base R only (no `dplyr`, `data.table` or other packages). Requirements: - `group_means(values, groups)` uses `tapply` and returns a named numeric vector of per-group means, whose names are the sorted unique group labels (character). It calls `stop()` if the lengths differ. - `group_summary(values, groups)` uses `tapply` and returns a `data.frame(stringsAsFactors = FALSE)` with columns `group` (character, ascending), `n` (integer), `mean` and `sd` (sample standard deviation, `NA` for singletons), and default `row.names` 1..k. It calls `stop()` if the lengths differ. - `column_classes(df)` uses `vapply` with a `character(1)` template and returns a named character vector holding the first `class()` of each column, in column order. - `numeric_means(df)` uses `vapply` with a `numeric(1)` template over the `is.numeric()` columns, in their original order, and returns named means computed with `na.rm = TRUE`. With no numeric columns it returns a length-0 named numeric vector. - Group labels are compared and sorted as character, so a factor `groups` gives the same result as its character form. The `vapply` templates are exact, so a result of the wrong type errors. Example: ```r values <- c(10, 20, 30, 40) groups <- c("b", "a", "b", "a") group_means(values, groups) # a b # 30 20 (named numeric, names sorted) group_summary(values, groups) # group n mean sd # 1 a 2 30 14.14214 # 2 b 2 20 14.14214 df <- data.frame(x = 1:3, y = c("p","q","r"), stringsAsFactors = FALSE) column_classes(df) # x y # "integer" "character" numeric_means(df) # x # 2 ``` ```hint Control the group order `tapply()` orders its output by the levels of the grouping factor, and a factor can carry levels that never occur in the data. Decide the levels yourself from the character labels. ``` ### Constraints & Assumptions - `values` is a numeric vector and `groups` is a character vector, a factor or another atomic vector. They must have the same length. - `df` is a data.frame with any mix of column types, possibly with no numeric columns at all. ### What a Strong Answer Covers - Length checks with `stop()` before any grouping happens. - Group labels compared and sorted as character, so a factor and its character form give identical output. - The exact output types: a plain named numeric vector, and a data frame with a character `group`, an integer `n`, a sample `sd` that is `NA` for singletons, and default row names. - `vapply()` templates that make a wrong result type or length fail loudly, plus a named empty numeric vector when no column is numeric. ### Follow-up Questions - Why prefer `vapply` over `sapply` here? - How would you handle groups containing `NA` labels? - How would you compute `n`, `mean` and `sd` in one pass over each group while keeping the output type-safe?

Overview: Implement base R helpers that compute per-group means and summaries with tapply and type-checked column reductions with vapply. It tests control of group order, exact output types such as integer counts and named empty vectors, and why vapply templates are safer than sapply.

General logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement base R helpers in /app/solution.R that compute grouped summaries with tapply and type-safe column reductions with vapply. Use base R only (no dplyr, data.table or other packages).

Requirements:

  • group_means(values, groups) uses tapply and returns a named numeric vector of per-group means, whose names are the sorted unique group labels (character). It calls stop() if the lengths differ.
  • group_summary(values, groups) uses tapply and returns a data.frame(stringsAsFactors = FALSE) with columns group (character, ascending), n (integer), mean and sd (sample standard deviation, NA for singletons), and default row.names 1..k. It calls stop() if the lengths differ.
  • column_classes(df) uses vapply with a character(1) template and returns a named character vector holding the first class() of each column, in column order.
  • numeric_means(df) uses vapply with a numeric(1) template over the is.numeric() columns, in their original order, and returns named means computed with na.rm = TRUE . With no numeric columns it returns a length-0 named numeric vector.
  • Group labels are compared and sorted as character, so a factor groups gives the same result as its character form. The vapply templates are exact, so a result of the wrong type errors.

Example:

values <- c(10, 20, 30, 40)
groups <- c("b", "a", "b", "a")

group_means(values, groups)
#   a  b
#  30 20   (named numeric, names sorted)

group_summary(values, groups)
#   group n mean       sd
# 1     a 2   30 14.14214
# 2     b 2   20 14.14214

df <- data.frame(x = 1:3, y = c("p","q","r"), stringsAsFactors = FALSE)
column_classes(df)
#         x           y
# "integer" "character"
numeric_means(df)
# x
# 2

Constraints & Assumptions

  • values is a numeric vector and groups is a character vector, a factor or another atomic vector. They must have the same length.
  • df is a data.frame with any mix of column types, possibly with no numeric columns at all.

What a Strong Answer Covers Guidance

  • Length checks with stop() before any grouping happens.
  • Group labels compared and sorted as character, so a factor and its character form give identical output.
  • The exact output types: a plain named numeric vector, and a data frame with a character group , an integer n , a sample sd that is NA for singletons, and default row names.
  • vapply() templates that make a wrong result type or length fail loudly, plus a named empty numeric vector when no column is numeric.

Follow-up Questions Guidance

  • Why prefer vapply over sapply here?
  • How would you handle groups containing NA labels?
  • How would you compute n , mean and sd in one pass over each group while keeping the output type-safe?