Missing Value Helpers in R: Count, Drop and Mean-Impute NA Values

Quick Overview

Write four base R helpers that count NA values in a vector, count them per data frame column, drop incomplete rows with clean row names, and replace missing values with the mean. It tests is.na() masks, integer return types, and attributes such as row.names and na.action.

Missing Value Helpers in R: Count, Drop and Mean-Impute NA Values

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement missing-data utilities in base R (`is.na()`, `na.omit()`) that clean or summarize `NA` values in vectors and data frames. - `count_missing(x)` takes an atomic vector and returns a single non-negative integer counting its `NA` values (use `is.na()`). - `missing_by_column(df)` returns a named integer vector of `NA` counts per column, with the column names as names, in the original column order. - `drop_missing_rows(df)` returns a data.frame of only the rows with no missing values, with `row.names` reset to the default sequential integers and no `na.action` attribute. - `impute_mean(x)` returns a numeric vector of the same length with every `NA` replaced by the mean of the non-`NA` values (`na.rm = TRUE`). Example: ```r count_missing(c(1, NA, 3, NA)) # 2 df <- data.frame(a = c(1, NA, 3), b = c(NA, NA, 6)) missing_by_column(df) # a=1, b=2 (named integer vector) drop_missing_rows(df) # only row 3, row.names reset to 1 impute_mean(c(2, NA, 4)) # c(2, 3, 4) ``` ```hint Masks and attributes `is.na()` returns a logical vector aligned with its input, and you can both sum a logical vector and index with one. If you use `na.omit()`, look at `attributes()` of what it returns. ``` ### Constraints & Assumptions - Base R only, with no packages. - `x` is an atomic vector (numeric for `impute_mean`), and `df` is a data.frame. ### Clarifying Questions to Ask - What should `impute_mean` return when every value is `NA`? - Should `NaN` values count as missing, the way `is.na()` treats them? ### What a Strong Answer Covers - Counting missing values with a logical mask and returning an integer. - A per-column count that keeps column names and order and has a guaranteed integer type. - Dropping incomplete rows while resetting row names and leaving no `na.action` attribute. - Mean imputation computed once from the observed values, including the all-`NA` case. - How `NaN`, integer input and one-column data frames behave. ### Follow-up Questions - Why use `vapply()` for `missing_by_column` instead of `sapply()` or `colSums(is.na(df))`? - What does mean imputation do to a column's variance and to its correlation with other columns, and what would you use instead? - How would you apply `impute_mean` to every numeric column of a data frame and leave the other columns alone?

Overview: Write four base R helpers that count NA values in a vector, count them per data frame column, drop incomplete rows with clean row names, and replace missing values with the mean. It tests is.na() masks, integer return types, and attributes such as row.names and na.action.

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

Implement missing-data utilities in base R (is.na(), na.omit()) that clean or summarize NA values in vectors and data frames.

  • count_missing(x) takes an atomic vector and returns a single non-negative integer counting its NA values (use is.na() ).
  • missing_by_column(df) returns a named integer vector of NA counts per column, with the column names as names, in the original column order.
  • drop_missing_rows(df) returns a data.frame of only the rows with no missing values, with row.names reset to the default sequential integers and no na.action attribute.
  • impute_mean(x) returns a numeric vector of the same length with every NA replaced by the mean of the non- NA values ( na.rm = TRUE ).

Example:

count_missing(c(1, NA, 3, NA))        # 2

df <- data.frame(a = c(1, NA, 3), b = c(NA, NA, 6))
missing_by_column(df)                 # a=1, b=2 (named integer vector)
drop_missing_rows(df)                 # only row 3, row.names reset to 1

impute_mean(c(2, NA, 4))              # c(2, 3, 4)

Constraints & Assumptions

  • Base R only, with no packages.
  • x is an atomic vector (numeric for impute_mean ), and df is a data.frame.

Clarifying Questions to Ask Guidance

  • What should impute_mean return when every value is NA ?
  • Should NaN values count as missing, the way is.na() treats them?

What a Strong Answer Covers Guidance

  • Counting missing values with a logical mask and returning an integer.
  • A per-column count that keeps column names and order and has a guaranteed integer type.
  • Dropping incomplete rows while resetting row names and leaving no na.action attribute.
  • Mean imputation computed once from the observed values, including the all- NA case.
  • How NaN , integer input and one-column data frames behave.

Follow-up Questions Guidance

  • Why use vapply() for missing_by_column instead of sapply() or colSums(is.na(df)) ?
  • What does mean imputation do to a column's variance and to its correlation with other columns, and what would you use instead?
  • How would you apply impute_mean to every numeric column of a data frame and leave the other columns alone?