Drop and Count NA Rows for One Column of an R Data Frame

Quick Overview

Write two base R helpers that drop the rows of a data frame where one column is NA and count the NA values in that column. It tests argument validation with stop(), logical row subsetting that keeps column types and order, and resetting row names after a filter.

Drop and Count NA Rows for One Column of an R Data Frame

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement two helpers in `/app/solution.R` that take a data frame `df` and a column name `col`. - `drop_na_rows(df, col)`: return a new data frame without the rows where `df[[col]]` is `NA`. - Preserve column types, `NA`s in other columns, and row order. Reset row names to `1..n`. - `count_na_in_column(df, col)`: return a single integer, the count of `NA` in `df[[col]]` only. - Both functions call `stop()` if `col` is not a length-1 character string or is not a column of `df`. Example: ```r df <- data.frame(a = c(1, NA, 3), b = c("x", "y", NA)) drop_na_rows(df, "a") # a b # 1 1 x # 2 3 <NA> count_na_in_column(df, "a") # [1] 1 ``` Row 2 is dropped because `a` is `NA` there. Row 3 is kept even though `b` is `NA`, and the row names are reset to 1 and 2. ```hint One mask, two uses Both functions can start from the same logical vector built from the target column. Watch what base R row subsetting does to row names, and to the result's shape when the data frame has a single column. ``` ### What a Strong Answer Covers - Validating `col` as a single character string that names a column of `df`, with a clear `stop()` message before any indexing. - Building the `NA` mask from the target column only, so `NA`s in other columns survive. - Subsetting rows with `drop = FALSE` so the result stays a data frame with its column types and row order. - Resetting row names to 1..n and returning a length-1 integer from the count. ### Follow-up Questions - How would you extend `drop_na_rows` to accept multiple columns? - Why reset row names, and how does base R subsetting handle them? - How do `NaN` values in a numeric column affect both functions, and what would you change if they should be kept?

Overview: Write two base R helpers that drop the rows of a data frame where one column is NA and count the NA values in that column. It tests argument validation with stop(), logical row subsetting that keeps column types and order, and resetting row names after a filter.

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

Implement two helpers in /app/solution.R that take a data frame df and a column name col.

  • drop_na_rows(df, col) : return a new data frame without the rows where df[[col]] is NA .
  • Preserve column types, NA s in other columns, and row order. Reset row names to 1..n .
  • count_na_in_column(df, col) : return a single integer, the count of NA in df[[col]] only.
  • Both functions call stop() if col is not a length-1 character string or is not a column of df .

Example:

df <- data.frame(a = c(1, NA, 3), b = c("x", "y", NA))

drop_na_rows(df, "a")
#   a    b
# 1 1    x
# 2 3 <NA>

count_na_in_column(df, "a")
# [1] 1

Row 2 is dropped because a is NA there. Row 3 is kept even though b is NA, and the row names are reset to 1 and 2.

What a Strong Answer Covers Guidance

  • Validating col as a single character string that names a column of df , with a clear stop() message before any indexing.
  • Building the NA mask from the target column only, so NA s in other columns survive.
  • Subsetting rows with drop = FALSE so the result stays a data frame with its column types and row order.
  • Resetting row names to 1..n and returning a length-1 integer from the count.

Follow-up Questions Guidance

  • How would you extend drop_na_rows to accept multiple columns?
  • Why reset row names, and how does base R subsetting handle them?
  • How do NaN values in a numeric column affect both functions, and what would you change if they should be kept?