Filter data frame rows by condition and return the top values in base R

Quick Overview

Write base R helpers that filter data frame rows by a threshold, a category set or an inclusive range, and return the n largest non-missing values of a vector. Tests logical indexing, NA handling, drop = FALSE, row name resets and edge cases for n.

Filter data frame rows by condition and return the top values in base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement base R helpers in `/app/solution.R` that filter rows of a `data.frame` and extract the largest numeric values from a vector. Use base R only. - `filter_above(df, col, threshold)` returns the rows where `df[[col]] > threshold` (strict). - `filter_category(df, col, categories)` returns the rows where `df[[col]] %in% categories`. - `filter_range(df, col, low, high)` returns the rows where `df[[col]] >= low` and `df[[col]] <= high` (inclusive on both ends). - `top_values(vec, n)` returns the `n` largest non-NA values of `vec`, sorted in descending order. The filters keep all columns and the original row order, and reset row names to `1..n`. ```r df <- data.frame(name = c("a", "b", "c"), score = c(10, 40, 25)) filter_above(df, "score", 20) # name score # 1 b 40 # 2 c 25 top_values(c(5, NA, 9, 2, 9), 2) # [1] 9 9 ``` ```hint Missing values Before using a comparison as a row index, check what `df[c(TRUE, NA, FALSE), ]` returns. ``` ### Constraints & Assumptions - Base R only, no packages. - `col` is a column name given as a string. - The filtered column may contain `NA` values, and `vec` may contain `NA` values. - `n` can be larger than the number of non-NA values in `vec`. ### What a Strong Answer Covers - Logical row indexing that keeps a one-column result as a data frame. - What happens to rows whose filter value is `NA`, and code that keeps them out of the result. - Resetting row names after filtering while keeping row order and columns. - `top_values` behavior with `NA` values, ties, and `n` larger than the available values. - The strict and inclusive comparisons exactly as specified. ### Follow-up Questions - How would you write these filters with `dplyr` or `data.table`, and how do those treat a condition that evaluates to `NA`? - How should `filter_category` behave when `categories` itself contains `NA`? - How would you get the top `n` values of a very long vector without sorting all of it? - How would you extend the filters to combine several conditions passed in at once?

Overview: Write base R helpers that filter data frame rows by a threshold, a category set or an inclusive range, and return the n largest non-missing values of a vector. Tests logical indexing, NA handling, drop = FALSE, row name resets and edge cases for n.

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

Implement base R helpers in /app/solution.R that filter rows of a data.frame and extract the largest numeric values from a vector. Use base R only.

  • filter_above(df, col, threshold) returns the rows where df[[col]] > threshold (strict).
  • filter_category(df, col, categories) returns the rows where df[[col]] %in% categories .
  • filter_range(df, col, low, high) returns the rows where df[[col]] >= low and df[[col]] <= high (inclusive on both ends).
  • top_values(vec, n) returns the n largest non-NA values of vec , sorted in descending order.

The filters keep all columns and the original row order, and reset row names to 1..n.

df <- data.frame(name = c("a", "b", "c"), score = c(10, 40, 25))
filter_above(df, "score", 20)
#   name score
# 1    b    40
# 2    c    25

top_values(c(5, NA, 9, 2, 9), 2)
# [1] 9 9

Constraints & Assumptions

  • Base R only, no packages.
  • col is a column name given as a string.
  • The filtered column may contain NA values, and vec may contain NA values.
  • n can be larger than the number of non-NA values in vec .

What a Strong Answer Covers Guidance

  • Logical row indexing that keeps a one-column result as a data frame.
  • What happens to rows whose filter value is NA , and code that keeps them out of the result.
  • Resetting row names after filtering while keeping row order and columns.
  • top_values behavior with NA values, ties, and n larger than the available values.
  • The strict and inclusive comparisons exactly as specified.

Follow-up Questions Guidance

  • How would you write these filters with dplyr or data.table , and how do those treat a condition that evaluates to NA ?
  • How should filter_category behave when categories itself contains NA ?
  • How would you get the top n values of a very long vector without sorting all of it?
  • How would you extend the filters to combine several conditions passed in at once?