Write type conversion helpers in base R for characters, numbers and factors

Quick Overview

Implement base R wrappers around as.character(), as.numeric() and as.factor() that convert factors by label, coerce quietly and count the NAs that failed parsing introduces. It tests how R stores factors and how to tell real parse failures from values that were already missing.

Write type conversion helpers in base R for characters, numbers and factors

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement type-coercion helpers in `/app/solution.R` that wrap `as.character()`, `as.numeric()` and `as.factor()` with predictable, quiet behaviour. Requirements: - `to_character(x)` returns a character vector of the same length as `x`. Factors return their labels, not their codes. - `to_numeric(x)` returns a double vector of the same length. Unparseable elements become `NA_real_`, with the warning suppressed (`suppressWarnings()`). Factors convert through their labels. - `to_factor(x, levels = NULL)` returns a factor whose levels are the sorted unique values of `x` when `levels` is `NULL`, and the supplied `levels` otherwise. - `count_na_introduced(x)` returns an integer scalar: the number of character elements that become `NA` after `as.numeric()` but were not already `NA` or `"NA"`. Example: ```r to_character(factor(c("a", "b"))) # c("a", "b") to_numeric(c("1.5", "x", "NA")) # c(1.5, NA, NA), no warning to_factor(c("b", "a")) # levels: a, b count_na_introduced(c("1", "x", "NA")) # 1L (only "x" is a real failure) ``` ```hint How factors are stored Before you convert a factor, check what `as.numeric(factor(c("10", "5")))` returns and why. ``` ### Constraints & Assumptions - Base R only, with no external packages. - `x` is an atomic vector or a factor. ### What a Strong Answer Covers - Factor inputs converted through their labels in both `to_character()` and `to_numeric()`. - Quiet numeric coercion that returns a double vector of the same length. - Correct handling of the `NULL` default for `levels`, and what happens to values that are missing from supplied levels. - An introduced-NA count that skips values already missing and the literal `"NA"`, returned as an integer scalar. ### Follow-up Questions - Why does `as.numeric()` on a factor give surprising results? - How would you distinguish a value that was already `NA` from one introduced by coercion? - `suppressWarnings()` hides every warning in the call. How would you silence only the coercion warning?

Overview: Implement base R wrappers around as.character(), as.numeric() and as.factor() that convert factors by label, coerce quietly and count the NAs that failed parsing introduces. It tests how R stores factors and how to tell real parse failures from values that were already missing.

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

Implement type-coercion helpers in /app/solution.R that wrap as.character(), as.numeric() and as.factor() with predictable, quiet behaviour.

Requirements:

  • to_character(x) returns a character vector of the same length as x . Factors return their labels, not their codes.
  • to_numeric(x) returns a double vector of the same length. Unparseable elements become NA_real_ , with the warning suppressed ( suppressWarnings() ). Factors convert through their labels.
  • to_factor(x, levels = NULL) returns a factor whose levels are the sorted unique values of x when levels is NULL , and the supplied levels otherwise.
  • count_na_introduced(x) returns an integer scalar: the number of character elements that become NA after as.numeric() but were not already NA or "NA" .

Example:

to_character(factor(c("a", "b")))        # c("a", "b")
to_numeric(c("1.5", "x", "NA"))          # c(1.5, NA, NA), no warning
to_factor(c("b", "a"))                   # levels: a, b
count_na_introduced(c("1", "x", "NA"))   # 1L  (only "x" is a real failure)

Constraints & Assumptions

  • Base R only, with no external packages.
  • x is an atomic vector or a factor.

What a Strong Answer Covers Guidance

  • Factor inputs converted through their labels in both to_character() and to_numeric() .
  • Quiet numeric coercion that returns a double vector of the same length.
  • Correct handling of the NULL default for levels , and what happens to values that are missing from supplied levels.
  • An introduced-NA count that skips values already missing and the literal "NA" , returned as an integer scalar.

Follow-up Questions Guidance

  • Why does as.numeric() on a factor give surprising results?
  • How would you distinguish a value that was already NA from one introduced by coercion?
  • suppressWarnings() hides every warning in the call. How would you silence only the coercion warning?