Implement Tukey's IQR outlier detection in R with per-group summaries

Quick Overview

Implement Tukey's 1.5×IQR outlier rule in R: compute quartile fences with NA values ignored, return and flag the outliers, and count outliers within each group. It tests quantile definitions, NA handling in comparisons and grouped calculations in base R.

Implement Tukey's IQR outlier detection in R with per-group summaries

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement outlier detection in R with Tukey's 1.5×IQR rule. Compute the quartiles with quantile type 7 (R's default) and ignore `NA` when computing the bounds. A value is an outlier if it is strictly below `lower` or strictly above `upper`. - `iqr_bounds(x)`: return a named list of numeric scalars `q1 = quantile(x, .25)`, `q3 = quantile(x, .75)`, `iqr = q3 - q1`, `lower = q1 - 1.5 * iqr` and `upper = q3 + 1.5 * iqr`, all computed with `na.rm = TRUE`. - `find_outliers(x)`: return the outlier values in their original order, or `numeric(0)` when there are none. `NA` is never an outlier. - `flag_outliers(df, col)`: return `df` unchanged plus a logical column `is_outlier`. Rows where the input is `NA` stay `NA`. - `outlier_summary(df, group_col, value_col)`: return one row per group, with bounds computed within that group, and columns `group`, `n` (the non-`NA` count) and `n_outliers`, sorted ascending by `group`. Example: ```r x <- c(10, 12, 11, 13, 12, 100, NA) iqr_bounds(x) # q1 = 11.25, q3 = 12.75, iqr = 1.5, lower = 9, upper = 15 (NA ignored) find_outliers(x) # 100 df <- data.frame(g = c("a", "a", "b"), v = c(1, 50, 2)) outlier_summary(df, "g", "v") # group n n_outliers # 1 a 2 0 # 2 b 1 0 ``` ```hint NA in comparisons A comparison with `NA` returns `NA`, and indexing with a logical vector that contains `NA` returns `NA` elements. Decide for each function whether those `NA` values should be kept or dropped. ``` ### Constraints & Assumptions - `col`, `group_col` and `value_col` are character scalars naming columns of `df`. - The values are numeric and may contain `NA`. - Plotting (a boxplot) is out of scope. ### What a Strong Answer Covers - Quartiles from `quantile()` type 7 with `NA` removed, and the fences derived from them - Strict inequalities and the required `NA` behavior in each of the three outputs - Bounds computed inside each group, a non-`NA` count, and output sorted by group - One shared outlier test reused by every function so the definition cannot drift - Edge cases such as an all-`NA` vector, a single-value group and an empty data frame ### Follow-up Questions - Why does the quantile type matter for the bounds? - How would you handle a group with fewer than 4 points? - When is the 1.5×IQR rule a poor choice, and what would you use instead?

Overview: Implement Tukey's 1.5×IQR outlier rule in R: compute quartile fences with NA values ignored, return and flag the outliers, and count outliers within each group. It tests quantile definitions, NA handling in comparisons and grouped calculations in base R.

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

Implement outlier detection in R with Tukey's 1.5×IQR rule. Compute the quartiles with quantile type 7 (R's default) and ignore NA when computing the bounds. A value is an outlier if it is strictly below lower or strictly above upper.

  • iqr_bounds(x) : return a named list of numeric scalars q1 = quantile(x, .25) , q3 = quantile(x, .75) , iqr = q3 - q1 , lower = q1 - 1.5 * iqr and upper = q3 + 1.5 * iqr , all computed with na.rm = TRUE .
  • find_outliers(x) : return the outlier values in their original order, or numeric(0) when there are none. NA is never an outlier.
  • flag_outliers(df, col) : return df unchanged plus a logical column is_outlier . Rows where the input is NA stay NA .
  • outlier_summary(df, group_col, value_col) : return one row per group, with bounds computed within that group, and columns group , n (the non- NA count) and n_outliers , sorted ascending by group .

Example:

x <- c(10, 12, 11, 13, 12, 100, NA)
iqr_bounds(x)      # q1 = 11.25, q3 = 12.75, iqr = 1.5, lower = 9, upper = 15 (NA ignored)
find_outliers(x)   # 100

df <- data.frame(g = c("a", "a", "b"), v = c(1, 50, 2))
outlier_summary(df, "g", "v")
#   group n n_outliers
# 1     a 2          0
# 2     b 1          0

Constraints & Assumptions

  • col , group_col and value_col are character scalars naming columns of df .
  • The values are numeric and may contain NA .
  • Plotting (a boxplot) is out of scope.

What a Strong Answer Covers Guidance

  • Quartiles from quantile() type 7 with NA removed, and the fences derived from them
  • Strict inequalities and the required NA behavior in each of the three outputs
  • Bounds computed inside each group, a non- NA count, and output sorted by group
  • One shared outlier test reused by every function so the definition cannot drift
  • Edge cases such as an all- NA vector, a single-value group and an empty data frame

Follow-up Questions Guidance

  • Why does the quantile type matter for the bounds?
  • How would you handle a group with fewer than 4 points?
  • When is the 1.5×IQR rule a poor choice, and what would you use instead?