Matrix Aggregations in R With apply()

Quick Overview

Implement four base R helpers that compute row sums, column means, row maxima and row-normalized values of a numeric matrix with apply(). It tests the margin argument, return types and names, and how apply() shapes its output when the applied function returns a vector.

Matrix Aggregations in R With apply()

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement four matrix aggregation helpers in base R using the `apply()` family. Each takes a numeric matrix and summarizes it along rows or columns. - `row_sums(m)` returns an unnamed numeric vector of row sums, of length `nrow(m)`. - `col_means(m)` returns a numeric vector of column means, of length `ncol(m)`. - `row_max(m)` returns a numeric vector of row maxima, of length `nrow(m)`. - `normalize_rows(m)` returns a matrix with the same dimensions as `m` in which each element is divided by its row sum, so every row sums to 1. Example: ```r m <- matrix(1:6, nrow = 2, byrow = TRUE) # rows: (1,2,3), (4,5,6) row_sums(m) # c(6, 15) col_means(m) # c(2.5, 3.5, 4.5) row_max(m) # c(3, 6) normalize_rows(m) # row 1: 1/6, 2/6, 3/6; row 2: 4/15, 5/15, 6/15 ``` ```hint Watch the output shape `apply(m, 1, f)` sends each row to `f`. When `f` returns a whole vector for every row, check `dim()` of what comes back before you return it from `normalize_rows`. ``` ### Constraints & Assumptions - `m` is a numeric matrix (integer or double values). - Use base R only, with no packages. ### Clarifying Questions to Ask - If `m` has row or column names, should `col_means` and `row_max` drop them like `row_sums` does? - What should `normalize_rows` return for a row that sums to zero? - Should missing values (`NA`) be skipped or allowed to propagate? ### What a Strong Answer Covers - Correct use of the margin argument for row versus column summaries. - Return types and lengths that match the spec, including how names end up on the result. - How `apply()` assembles its output when the function returns a vector, and what that means for the shape of `normalize_rows`. - Edge cases such as zero-sum rows, `NA` values and integer input. - When the vectorized built-ins are a better choice than `apply()`. ### Follow-up Questions - When does it matter whether you use `apply()` or `rowSums()` and `colMeans()`? - Why does `apply(m, 2, f)` sometimes return a matrix instead of a vector? - How would you write `normalize_rows` with `sweep()`, and how would you normalize columns instead of rows?

Overview: Implement four base R helpers that compute row sums, column means, row maxima and row-normalized values of a numeric matrix with apply(). It tests the margin argument, return types and names, and how apply() shapes its output when the applied function returns a vector.

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

Implement four matrix aggregation helpers in base R using the apply() family. Each takes a numeric matrix and summarizes it along rows or columns.

  • row_sums(m) returns an unnamed numeric vector of row sums, of length nrow(m) .
  • col_means(m) returns a numeric vector of column means, of length ncol(m) .
  • row_max(m) returns a numeric vector of row maxima, of length nrow(m) .
  • normalize_rows(m) returns a matrix with the same dimensions as m in which each element is divided by its row sum, so every row sums to 1.

Example:

m <- matrix(1:6, nrow = 2, byrow = TRUE)  # rows: (1,2,3), (4,5,6)
row_sums(m)        # c(6, 15)
col_means(m)       # c(2.5, 3.5, 4.5)
row_max(m)         # c(3, 6)
normalize_rows(m)  # row 1: 1/6, 2/6, 3/6; row 2: 4/15, 5/15, 6/15

Constraints & Assumptions

  • m is a numeric matrix (integer or double values).
  • Use base R only, with no packages.

Clarifying Questions to Ask Guidance

  • If m has row or column names, should col_means and row_max drop them like row_sums does?
  • What should normalize_rows return for a row that sums to zero?
  • Should missing values ( NA ) be skipped or allowed to propagate?

What a Strong Answer Covers Guidance

  • Correct use of the margin argument for row versus column summaries.
  • Return types and lengths that match the spec, including how names end up on the result.
  • How apply() assembles its output when the function returns a vector, and what that means for the shape of normalize_rows .
  • Edge cases such as zero-sum rows, NA values and integer input.
  • When the vectorized built-ins are a better choice than apply() .

Follow-up Questions Guidance

  • When does it matter whether you use apply() or rowSums() and colMeans() ?
  • Why does apply(m, 2, f) sometimes return a matrix instead of a vector?
  • How would you write normalize_rows with sweep() , and how would you normalize columns instead of rows?