Return a data frame's dimensions, column classes, missing counts and unique counts in R
Quick Overview
Write three R functions that return a data frame's dimensions, each column's class and a per-column summary of missing and unique counts. It tests named vectors, exact output types with vapply(), how unique() treats NA, and keeping column order intact.
Return a data frame's dimensions, column classes, missing counts and unique counts in R
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement three functions in `/app/solution.R` that return (not print) a data frame's dimensions, per-column classes and a structural summary. Preserve column order in every output.
- `get_dimensions(df)` returns the named integer vector `c(rows = <nrow>, cols = <ncol>)`.
- `column_classes(df)` returns a named character vector whose names are the column names and whose values are `class(col)[1]`.
- `describe_structure(df)` returns a data.frame built with `stringsAsFactors = FALSE`, with row names `1..ncol` and one row per column of `df`. It has exactly the columns `variable`, `class`, `n_missing` and `n_unique`:
- `variable` is the column name (character).
- `class` is `class(col)[1]` (character).
- `n_missing` is the integer count of `NA` values.
- `n_unique` is the integer `length(unique(col))`, where `NA` counts as a value.
For example:
```r
df <- data.frame(
id = c(1L, 2L, 3L),
name = c("a", NA, "a"),
stringsAsFactors = FALSE
)
get_dimensions(df)
#> rows cols
#> 3 2
column_classes(df)
#> id name
#> "integer" "character"
describe_structure(df)
#> variable class n_missing n_unique
#> 1 id integer 0 3
#> 2 name character 1 2
```
```hint Check every type
Several base functions silently return doubles or drop names. Check the type and the names of each intermediate vector before you assemble the result.
```
### What a Strong Answer Covers
- Returning values with the exact names and types each output requires, without printing.
- Keeping the original column order in all three outputs.
- Counting missing and unique values, including how `unique()` treats `NA`.
- Columns whose class has more than one element, and data frames with zero rows or zero columns.
### Follow-up Questions
- How would `n_unique` change if `NA` should not count as a value?
- Why are `str()` and `summary()` a poor fit when the result is checked by other code?
- How should `describe_structure()` handle a factor column or a list column?
Overview: Write three R functions that return a data frame's dimensions, each column's class and a per-column summary of missing and unique counts. It tests named vectors, exact output types with vapply(), how unique() treats NA, and keeping column order intact.
Implement three functions in /app/solution.R that return (not print) a data frame's dimensions, per-column classes and a structural summary. Preserve column order in every output.
get_dimensions(df)
returns the named integer vector
c(rows = <nrow>, cols = <ncol>)
.
column_classes(df)
returns a named character vector whose names are the column names and whose values are
class(col)[1]
.
describe_structure(df)
returns a data.frame built with
stringsAsFactors = FALSE
, with row names
1..ncol
and one row per column of
df
. It has exactly the columns
variable
,
class
,
n_missing
and
n_unique
:
variable
is the column name (character).
class
is
class(col)[1]
(character).
n_missing
is the integer count of
NA
values.
n_unique
is the integer
length(unique(col))
, where
NA
counts as a value.
For example:
df <- data.frame(
id = c(1L, 2L, 3L),
name = c("a", NA, "a"),
stringsAsFactors = FALSE
)
get_dimensions(df)
#> rows cols
#> 3 2
column_classes(df)
#> id name
#> "integer" "character"
describe_structure(df)
#> variable class n_missing n_unique
#> 1 id integer 0 3
#> 2 name character 1 2
What a Strong Answer Covers Guidance
Returning values with the exact names and types each output requires, without printing.
Keeping the original column order in all three outputs.
Counting missing and unique values, including how
unique()
treats
NA
.
Columns whose class has more than one element, and data frames with zero rows or zero columns.
Follow-up Questions Guidance
How would
n_unique
change if
NA
should not count as a value?
Why are
str()
and
summary()
a poor fit when the result is checked by other code?
How should
describe_structure()
handle a factor column or a list column?