Implement a weighted group summary and a winsorized mean in base R
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement two aggregation helpers in `/app/solution.R` using base R only, with no side effects (no printing, no writing files).
- `weighted_group_summary(df, group_col, value_col, weight_col)`: `df` is a data.frame, the other three are length-1 character column names. Drop rows where `df[[value_col]]` or `df[[weight_col]]` is `NA`, then group by distinct values of `group_col`.
- Per group compute the weighted mean `sum(value*weight)/sum(weight)`, the row count `n`, and `total_weight = sum(weight)`.
- Return a data.frame with columns in exact order `group` (sorted ascending), `weighted_mean` (numeric), `n` (integer), `total_weight` (numeric), default integer row names, `stringsAsFactors = FALSE`.
- `winsorized_mean(x, lower = 0.05, upper = 0.95)`: `x` is numeric (may contain `NA`), `lower` and `upper` are length-1 fractions in [0, 1] with `lower < upper`.
- Drop `NA`, compute `ql = quantile(x, lower)` and `qu = quantile(x, upper)` (default type 7), clamp values to [`ql`, `qu`], and return the mean as a single unnamed numeric scalar.
Example:
```r
df <- data.frame(
g = c("b", "a", "a", "b"),
v = c(10, 4, NA, 8),
w = c(1, 2, 3, 1)
)
weighted_group_summary(df, "g", "v", "w")
# group weighted_mean n total_weight
# 1 a 4 1 2
# 2 b 9 2 2
winsorized_mean(c(1, 2, 3, 4, 100), lower = 0.05, upper = 0.95)
# [1] 18.2
```
```hint Filter once
Build a single keep mask from the value and weight columns before you look at groups, so the weighted sum, the count and the total weight all describe the same rows.
```
```hint Watch the attributes
Several base R helpers attach names to their results (`vapply` over a character vector, `quantile`). Check where those names end up, because a named column can turn into row names.
```
### Clarifying Questions to Ask
- If `group_col` itself contains `NA`, should those rows form their own group or be left out?
- What should a group report when its weights sum to zero?
- What should each function return when nothing is left after dropping `NA` values?
### What a Strong Answer Covers
- Dropping rows with a missing value or weight once, before grouping, and computing all three per-group statistics from the same rows.
- Matching the return contract exactly: column order, ascending groups, integer `n`, numeric columns, plain `1, 2, ...` row names.
- Computing type-7 quantiles on the NA-free vector and clamping before averaging.
- Edge cases: zero total weight, `NA` in the group column, factor groups, and inputs that are empty after filtering.
### Follow-up Questions
- How would you extend `weighted_group_summary` to aggregate several value columns at once?
- Why might the `quantile` type matter for the winsorized mean?
- How would you make the group summary fast for millions of rows and tens of thousands of groups?
- How does winsorizing differ from trimming, and when would you prefer one over the other?
Overview: Write two base R helpers: a grouped weighted mean that also reports row counts and total weight, and a winsorized mean that clamps values at type-7 quantiles. Tests NA handling, exact data.frame return contracts and careful use of quantile.