Dataset Cleaning Pipeline in Base R

Quick Overview

Write four base R functions that standardize text columns, fill missing numeric values with column means, drop duplicate rows by key, and chain the steps in a fixed order. It tests vectorized string handling, NA semantics, and how step order changes which rows survive and which mean is used.

Dataset Cleaning Pipeline in Base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write four functions in base R (no external packages) that clean a data frame: a text standardizer, a numeric imputer, a de-duplicator, and `clean_dataset`, which chains the other three in a fixed order. - `standardize_text(x)`: for a character or factor vector, return a character vector of the same length in which each value is trimmed, internal runs of whitespace are collapsed to one space, and letters are lowercased. `NA` stays `NA`. - `impute_numeric(df)`: return a data frame of the same shape in which each `NA` in a numeric column is replaced by that column's mean (`na.rm = TRUE`). Non-numeric columns are unchanged. - `dedupe(df, keys)`: keep the first occurrence of each unique combination of the `keys` columns, preserve row order, and reset row names to `1..n`. - `clean_dataset(df, text_cols, key_cols)`: apply `standardize_text` to each column in `text_cols`, then `impute_numeric`, then `dedupe` on `key_cols`. Reset row names. Example: ```r df <- data.frame( name = c(" Alice ", "alice", "BOB\tSmith"), age = c(30, NA, 40), stringsAsFactors = FALSE ) clean_dataset(df, text_cols = "name", key_cols = "name") # name age # 1 alice 30 # 2 bob smith 40 ``` The second row becomes a duplicate of the first once its name is standardized, so it is dropped. Its missing age was filled with the mean of 30 and 40 (35) before the dedupe step ran. ```hint Find the right built-ins Base R already has vectorized functions for trimming, regex replacement, case folding and flagging repeated rows of a data frame. Check how each one treats `NA`. ``` ```hint Watch the edge columns Ask what the column mean is when a numeric column has no observed values, and what happens to an integer column's type when you assign a mean into it. ``` ### Constraints & Assumptions - Base R only, no packages. - `text_cols` and `key_cols` are character vectors naming columns that exist in `df`, and `key_cols` has at least one name. - Columns may be character, factor, integer, double or logical. ### Clarifying Questions to Ask - If a numeric column is entirely `NA`, should it stay `NA`, become `NaN`, or take a default value? - Should integer columns stay integer after imputation (for example by rounding the mean), or is converting them to double acceptable? - Which characters count as whitespace: only spaces and tabs, or also newlines and non-breaking spaces? - Do two rows whose key is `NA` count as duplicates of each other? ### What a Strong Answer Covers - Text cleaning that is vectorized, keeps the vector length and the `NA` positions, and accepts factors - Imputation limited to numeric columns, with a stated choice for columns that have no observed values - First-occurrence de-duplication on a multi-column key, with row order kept and row names reset - The step order inside `clean_dataset`, and how changing it changes which rows collapse and which mean is used - The cost of each step and edge cases such as empty data frames and `NA` keys ### Follow-up Questions - How would you make `impute_numeric` fall back when a whole column is `NA`? - Why does the order of the steps (text, then impute, then dedupe) matter? - How would you change `dedupe` to keep the most recent row per key, given a timestamp column? - How would you impute with a group mean, such as the mean age within each department, in base R?

Overview: Write four base R functions that standardize text columns, fill missing numeric values with column means, drop duplicate rows by key, and chain the steps in a fixed order. It tests vectorized string handling, NA semantics, and how step order changes which rows survive and which mean is used.

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

Write four functions in base R (no external packages) that clean a data frame: a text standardizer, a numeric imputer, a de-duplicator, and clean_dataset, which chains the other three in a fixed order.

  • standardize_text(x) : for a character or factor vector, return a character vector of the same length in which each value is trimmed, internal runs of whitespace are collapsed to one space, and letters are lowercased. NA stays NA .
  • impute_numeric(df) : return a data frame of the same shape in which each NA in a numeric column is replaced by that column's mean ( na.rm = TRUE ). Non-numeric columns are unchanged.
  • dedupe(df, keys) : keep the first occurrence of each unique combination of the keys columns, preserve row order, and reset row names to 1..n .
  • clean_dataset(df, text_cols, key_cols) : apply standardize_text to each column in text_cols , then impute_numeric , then dedupe on key_cols . Reset row names.

Example:

df <- data.frame(
  name = c("  Alice  ", "alice", "BOB\tSmith"),
  age  = c(30, NA, 40),
  stringsAsFactors = FALSE
)

clean_dataset(df, text_cols = "name", key_cols = "name")
#        name age
# 1     alice  30
# 2 bob smith  40

The second row becomes a duplicate of the first once its name is standardized, so it is dropped. Its missing age was filled with the mean of 30 and 40 (35) before the dedupe step ran.

Constraints & Assumptions

  • Base R only, no packages.
  • text_cols and key_cols are character vectors naming columns that exist in df , and key_cols has at least one name.
  • Columns may be character, factor, integer, double or logical.

Clarifying Questions to Ask Guidance

  • If a numeric column is entirely NA , should it stay NA , become NaN , or take a default value?
  • Should integer columns stay integer after imputation (for example by rounding the mean), or is converting them to double acceptable?
  • Which characters count as whitespace: only spaces and tabs, or also newlines and non-breaking spaces?
  • Do two rows whose key is NA count as duplicates of each other?

What a Strong Answer Covers Guidance

  • Text cleaning that is vectorized, keeps the vector length and the NA positions, and accepts factors
  • Imputation limited to numeric columns, with a stated choice for columns that have no observed values
  • First-occurrence de-duplication on a multi-column key, with row order kept and row names reset
  • The step order inside clean_dataset , and how changing it changes which rows collapse and which mean is used
  • The cost of each step and edge cases such as empty data frames and NA keys

Follow-up Questions Guidance

  • How would you make impute_numeric fall back when a whole column is NA ?
  • Why does the order of the steps (text, then impute, then dedupe) matter?
  • How would you change dedupe to keep the most recent row per key, given a timestamp column?
  • How would you impute with a group mean, such as the mean age within each department, in base R?