Standardize names, emails and phone numbers in a contact data frame with base R

Quick Overview

Write base R helpers that clean whitespace, title-case names, lower-case emails and format 10-digit phone numbers, then apply them to the columns of a contact data frame. It tests vectorized regex with gsub, NA handling, and matching a precise definition of a word.

Standardize names, emails and phone numbers in a contact data frame with base R

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Implement base R helpers that normalize contact data (names, emails and phone numbers). Each vector function takes a character vector and preserves its length, element order and `NA` values. A final function applies them column-wise to a data frame. Use no external packages. - `clean_whitespace(x)` trims leading and trailing whitespace and collapses every internal whitespace run to a single space. - `to_title_case(x)` applies `clean_whitespace`, lower-cases, then capitalizes only the first character of each word, where a word is a run of non-whitespace characters. - `standardize_phone(x)` strips non-digits, formats exactly 10 digits as `"(XXX) XXX-XXXX"`, and returns `NA_character_` otherwise. - `standardize_email(x)` trims and lower-cases. - `standardize_contacts(df)` returns a `data.frame` (`stringsAsFactors = FALSE`) with columns `name`, `email` and `phone` in that order, built by applying `to_title_case`, `standardize_email` and `standardize_phone` respectively. For example: ```r clean_whitespace(" hello world ") # "hello world" to_title_case("john DOE") # "John Doe" standardize_phone("1234567890") # "(123) 456-7890" standardize_phone("123-456") # NA standardize_email(" John@Ex.COM ") # "john@ex.com" ``` ```hint Test the word rule Run your capitalization on `"mary-jane o'neil"`. Regex word boundaries and the spec's whitespace-delimited words disagree on that input. ``` ```hint Empty and missing input Check what `standardize_phone` returns for `NA` and for `character(0)`, both in value and in type. ``` ### Constraints & Assumptions - Inputs are character vectors that may contain `NA`. `df` has the columns `name`, `email` and `phone`. - Base R only. - Every vector function returns a character vector of the same length as its input. ### What a Strong Answer Covers - Vectorized string functions that keep length, order and `NA` without per-element loops. - A capitalization rule that matches the whitespace-based definition of a word, including words with hyphens, apostrophes or a leading digit. - A phone rule that counts digits after stripping and returns `NA_character_` in every other case, including `NA` input. - A `standardize_contacts` that returns exactly the three columns, in order, as character columns. ### Follow-up Questions - How would you handle 11-digit numbers with a leading country code (for example `"1XXXXXXXXXX"`)? - How would you make `to_title_case` vectorized and efficient for large inputs? - Lower-casing the whole email is the requested behavior. When could it change the meaning of an address, and how would you guard against that?

Overview: Write base R helpers that clean whitespace, title-case names, lower-case emails and format 10-digit phone numbers, then apply them to the columns of a contact data frame. It tests vectorized regex with gsub, NA handling, and matching a precise definition of a word.

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

Implement base R helpers that normalize contact data (names, emails and phone numbers). Each vector function takes a character vector and preserves its length, element order and NA values. A final function applies them column-wise to a data frame. Use no external packages.

  • clean_whitespace(x) trims leading and trailing whitespace and collapses every internal whitespace run to a single space.
  • to_title_case(x) applies clean_whitespace , lower-cases, then capitalizes only the first character of each word, where a word is a run of non-whitespace characters.
  • standardize_phone(x) strips non-digits, formats exactly 10 digits as "(XXX) XXX-XXXX" , and returns NA_character_ otherwise.
  • standardize_email(x) trims and lower-cases.
  • standardize_contacts(df) returns a data.frame ( stringsAsFactors = FALSE ) with columns name , email and phone in that order, built by applying to_title_case , standardize_email and standardize_phone respectively.

For example:

clean_whitespace("  hello   world ")   # "hello world"
to_title_case("john  DOE")            # "John Doe"
standardize_phone("1234567890")       # "(123) 456-7890"
standardize_phone("123-456")          # NA
standardize_email(" John@Ex.COM ")    # "john@ex.com"

Constraints & Assumptions

  • Inputs are character vectors that may contain NA . df has the columns name , email and phone .
  • Base R only.
  • Every vector function returns a character vector of the same length as its input.

What a Strong Answer Covers Guidance

  • Vectorized string functions that keep length, order and NA without per-element loops.
  • A capitalization rule that matches the whitespace-based definition of a word, including words with hyphens, apostrophes or a leading digit.
  • A phone rule that counts digits after stripping and returns NA_character_ in every other case, including NA input.
  • A standardize_contacts that returns exactly the three columns, in order, as character columns.

Follow-up Questions Guidance

  • How would you handle 11-digit numbers with a leading country code (for example "1XXXXXXXXXX" )?
  • How would you make to_title_case vectorized and efficient for large inputs?
  • Lower-casing the whole email is the requested behavior. When could it change the meaning of an address, and how would you guard against that?