Dataset Quality Validation Functions in R: Completeness, Ranges and Key Uniqueness
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement three data-quality checks in R (completeness, value ranges, composite-key uniqueness) and a `validate_dataset` driver that returns a summary `data.frame`. All counts must be integer type.
- `check_completeness(df, required_cols)`: return a named integer vector of `NA` counts (names are `required_cols`, in the same order). Call `stop()` if any name is not a column of `df`.
- `check_ranges(df, ranges)`: `ranges` is a named list of `c(min, max)`. Return a named integer vector (same order) counting non-`NA` values strictly below min or strictly above max. Boundary values are valid and `NA` values are ignored.
- `check_uniqueness(df, key_cols)`: return an integer scalar counting duplicate rows (occurrences beyond the first) over `key_cols` only.
- `validate_dataset(df, rules)`: `rules` may hold `$required_cols`, `$ranges` and `$unique_keys`; an absent or `NULL` entry is skipped. Return a `data.frame` (with `stringsAsFactors = FALSE`) with row names `1..n` and these columns: `check` (`"completeness"`, `"range"` or `"uniqueness"`), `target` (the column name, or the key columns joined with `"+"`, such as `"id+date"`), `n_violations` (integer) and `passed` (logical, `n_violations == 0`).
- Row order: completeness rows in `required_cols` order, then range rows in `ranges` order, then one uniqueness row if requested.
Example:
```r
df <- data.frame(
id = c(1L, 2L, 2L, 3L, 4L),
age = c(25, NA, 130, -5, 120)
)
rules <- list(
required_cols = c("id", "age"),
ranges = list(age = c(0, 120)),
unique_keys = c("id")
)
validate_dataset(df, rules)
# check target n_violations passed
# 1 completeness id 0 TRUE
# 2 completeness age 1 FALSE
# 3 range age 2 FALSE
# 4 uniqueness id 1 FALSE
```
`age` has one `NA`. Two ages fall outside 0 to 120 (130 and -5); 120 sits on the boundary and is valid, and the `NA` is ignored by the range check. `id` 2 appears twice, which is one occurrence beyond the first.
```hint Count with logical vectors
Summing a logical vector gives an integer count. Work out what a comparison returns for an `NA` value before you sum it.
```
```hint Plan the driver's output
Build the summary from plain vectors, and decide what the driver should return when every rule is absent.
```
### Constraints & Assumptions
- Base R is enough; `dplyr` is also available.
- Counts must be integer type, not double.
- Each `ranges` entry is a numeric `c(min, max)`, and both bounds are inclusive.
### Clarifying Questions to Ask
- Should a range or key column that is missing from `df` raise an error, as completeness does, or count as zero violations?
- How should a one-sided range, such as a minimum only, be written?
- Should rows with `NA` in a key column count toward duplicates?
- What should `validate_dataset` return when no rules are supplied?
### What a Strong Answer Covers
- Helpers that return exactly the shapes asked for: named integer vectors in input order and an integer scalar
- `NA` handling that counts missing values for completeness and ignores them for ranges, with inclusive bounds
- Duplicate counting over the key columns only, counting repeats beyond the first occurrence
- A driver that skips absent rules, keeps the required row order, types every column correctly and resets row names
- Edge cases such as no rules, zero-row data and misnamed columns
### Follow-up Questions
- How would you extend the summary to report a percentage of rows violated?
- How would you make the check set extensible without editing `validate_dataset`?
- How would you return the offending row indices for each failed check?
Overview: Implement R functions that count missing values in required columns, values outside inclusive numeric ranges, and duplicate rows on a composite key, then combine them into one typed summary data frame. It tests NA handling, integer types, row ordering and edge cases in base R.