Write dplyr helpers that take bare column names with rlang tidy evaluation

Quick Overview

Write three dplyr helpers in R that take bare column names, capture them with enquo, enquos and ensym, and splice them into group_by, summarise, select and mutate. Tests tidy evaluation, quasiquotation with the := operator, data masking and how missing columns are reported.

Write dplyr helpers that take bare column names with rlang tidy evaluation

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement three helpers in `/app/solution.R` that accept bare (unquoted) column names through `rlang` tidy evaluation (`enquo`, `enquos`, `ensym`) and splice them into dplyr verbs with `!!`, `!!!` and `:=`. - `summarise_by(df, group_col, value_col)` returns a tibble with the grouping column (under the same name) and `mean_value` (double, computed with `na.rm = TRUE`). It has one row per group, sorted ascending by group. - `pick_columns(df, ...)` captures the column names with `enquos` and returns a tibble with only those columns, in the order supplied. - `mutate_ratio(df, numerator, denominator, new_col)` captures the first two arguments with `enquo` and `new_col` with `ensym`, and returns `df` as a tibble with a new column named by `new_col` (set through `:=`) equal to the element-wise `numerator / denominator`. - All three raise an error when a referenced column does not exist. ```r df <- tibble(grp = c("a", "b", "a"), x = c(1, 2, 3), y = c(2, 2, 4)) summarise_by(df, grp, x) #> # A tibble: 2 x 2 #> grp mean_value #> <chr> <dbl> #> 1 a 2 #> 2 b 2 pick_columns(df, y, grp) # only y and grp, in that order mutate_ratio(df, x, y, ratio) # adds ratio = x / y ``` ```hint Where names are looked up Consider what `group_by(group_col)` would do inside the function body: which name does dplyr look for, and in which data frame or environment? ``` ```hint Naming the output R does not let you compute the name on the left of `=` in a function call. Check what the left side of `:=` accepts. ``` ### Constraints & Assumptions - `rlang`, `dplyr` and `tibble` are available. - The helpers must use `enquo`, `enquos` and `ensym` with `!!`, `!!!` and `:=`. - Callers pass bare column names, for example `summarise_by(df, grp, x)`. ### What a Strong Answer Covers - Capturing the caller's arguments as quosures or symbols and unquoting them inside the dplyr verbs. - A grouped summary with one row per group, `NA` removal, grouping dropped and ascending order. - Variadic column selection that keeps the supplied order. - Dynamic naming of the new column with `:=`, and why a symbol fits the target name. - Behavior when a column is missing, including how the data mask can fall back to a variable in the caller's environment, plus edge cases such as a zero denominator. ### Follow-up Questions - Why prefer `ensym` over `enquo` for `new_col`? - How would you also let callers pass column names as strings or character vectors? - How would you write the same helpers with the embrace operator `{{ }}`? - A caller has a variable `z` in their workspace and calls `mutate_ratio(df, z, y, r)` on a data frame without a `z` column. What happens, and how would you make the helper strict?

Overview: Write three dplyr helpers in R that take bare column names, capture them with enquo, enquos and ensym, and splice them into group_by, summarise, select and mutate. Tests tidy evaluation, quasiquotation with the := operator, data masking and how missing columns are reported.

Nov 25, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement three helpers in /app/solution.R that accept bare (unquoted) column names through rlang tidy evaluation (enquo, enquos, ensym) and splice them into dplyr verbs with !!, !!! and :=.

  • summarise_by(df, group_col, value_col) returns a tibble with the grouping column (under the same name) and mean_value (double, computed with na.rm = TRUE ). It has one row per group, sorted ascending by group.
  • pick_columns(df, ...) captures the column names with enquos and returns a tibble with only those columns, in the order supplied.
  • mutate_ratio(df, numerator, denominator, new_col) captures the first two arguments with enquo and new_col with ensym , and returns df as a tibble with a new column named by new_col (set through := ) equal to the element-wise numerator / denominator .
  • All three raise an error when a referenced column does not exist.
df <- tibble(grp = c("a", "b", "a"), x = c(1, 2, 3), y = c(2, 2, 4))

summarise_by(df, grp, x)
#> # A tibble: 2 x 2
#>   grp   mean_value
#>   <chr>      <dbl>
#> 1 a            2
#> 2 b            2

pick_columns(df, y, grp)        # only y and grp, in that order

mutate_ratio(df, x, y, ratio)   # adds ratio = x / y

Constraints & Assumptions

  • rlang , dplyr and tibble are available.
  • The helpers must use enquo , enquos and ensym with !! , !!! and := .
  • Callers pass bare column names, for example summarise_by(df, grp, x) .

What a Strong Answer Covers Guidance

  • Capturing the caller's arguments as quosures or symbols and unquoting them inside the dplyr verbs.
  • A grouped summary with one row per group, NA removal, grouping dropped and ascending order.
  • Variadic column selection that keeps the supplied order.
  • Dynamic naming of the new column with := , and why a symbol fits the target name.
  • Behavior when a column is missing, including how the data mask can fall back to a variable in the caller's environment, plus edge cases such as a zero denominator.

Follow-up Questions Guidance

  • Why prefer ensym over enquo for new_col ?
  • How would you also let callers pass column names as strings or character vectors?
  • How would you write the same helpers with the embrace operator {{ }} ?
  • A caller has a variable z in their workspace and calls mutate_ratio(df, z, y, r) on a data frame without a z column. What happens, and how would you make the helper strict?