Concatenate Strings in R with paste, paste0 and gsub

Quick Overview

Implement four base R string helpers: join first and last names element-wise, collapse a vector into one string, prefix every element, and build a lowercase hyphenated slug. Tests the difference between sep and collapse in paste, argument recycling, and regex replacement with gsub.

Concatenate Strings in R with paste, paste0 and gsub

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement four base R string helpers using tools like `paste`, `paste0` and `gsub`. Do not use external packages. - `full_name(first, last)` is vectorized over equal-length character vectors and joins each pair with a single space. - `join_with(x, sep)` collapses `x` into a length-1 string with `sep` between elements. - `add_prefix(prefix, x)` prepends the length-1 `prefix` to each element of `x` with no separator and returns a character vector. - `make_slug(text)` lowercases the text, replaces each run of one or more spaces with a single `-`, and returns a length-1 string. For example: ```r full_name(c("Ada", "Alan"), c("Lovelace", "Turing")) # -> c("Ada Lovelace", "Alan Turing") join_with(c("a", "b", "c"), ", ") # -> "a, b, c" add_prefix("Dr. ", c("Ada", "Alan")) # -> c("Dr. Ada", "Dr. Alan") make_slug("Hello World") # -> "hello-world" ``` ```hint Length in, length out Three of these helpers return one string per input element and one reduces a whole vector to a single string. Check which argument of `paste` controls each behavior. ``` ### Constraints & Assumptions - Base R only, no packages. - `first` and `last` have the same length. - `prefix` and `sep` are length-1 character strings. - `make_slug` receives a single string, and a space means the literal space character. ### What a Strong Answer Covers - The difference between `sep`, which joins arguments element by element, and `collapse`, which reduces a vector to one string. - How R recycles a length-1 argument such as `prefix` across a longer vector. - A regular expression with a quantifier so that a run of spaces becomes one hyphen. - Edge cases: empty vectors, `NA` values, vectors of unequal length, and spaces at the start or end of the slug text. ### Follow-up Questions - What does `add_prefix("Dr. ", character(0))` return, and how would you make it return `character(0)`? - How would you change `make_slug` to trim the ends, treat any whitespace as a separator, and drop punctuation? - `paste` turns `NA` into the string `"NA"`. How would you make `full_name` return `NA` when either part is missing? - What happens if `full_name` gets vectors of different lengths, and how would you guard against it?

Overview: Implement four base R string helpers: join first and last names element-wise, collapse a vector into one string, prefix every element, and build a lowercase hyphenated slug. Tests the difference between sep and collapse in paste, argument recycling, and regex replacement with gsub.

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

Implement four base R string helpers using tools like paste, paste0 and gsub. Do not use external packages.

  • full_name(first, last) is vectorized over equal-length character vectors and joins each pair with a single space.
  • join_with(x, sep) collapses x into a length-1 string with sep between elements.
  • add_prefix(prefix, x) prepends the length-1 prefix to each element of x with no separator and returns a character vector.
  • make_slug(text) lowercases the text, replaces each run of one or more spaces with a single - , and returns a length-1 string.

For example:

full_name(c("Ada", "Alan"), c("Lovelace", "Turing"))
# -> c("Ada Lovelace", "Alan Turing")

join_with(c("a", "b", "c"), ", ")
# -> "a, b, c"

add_prefix("Dr. ", c("Ada", "Alan"))
# -> c("Dr. Ada", "Dr. Alan")

make_slug("Hello   World")
# -> "hello-world"

Constraints & Assumptions

  • Base R only, no packages.
  • first and last have the same length.
  • prefix and sep are length-1 character strings.
  • make_slug receives a single string, and a space means the literal space character.

What a Strong Answer Covers Guidance

  • The difference between sep , which joins arguments element by element, and collapse , which reduces a vector to one string.
  • How R recycles a length-1 argument such as prefix across a longer vector.
  • A regular expression with a quantifier so that a run of spaces becomes one hyphen.
  • Edge cases: empty vectors, NA values, vectors of unequal length, and spaces at the start or end of the slug text.

Follow-up Questions Guidance

  • What does add_prefix("Dr. ", character(0)) return, and how would you make it return character(0) ?
  • How would you change make_slug to trim the ends, treat any whitespace as a separator, and drop punctuation?
  • paste turns NA into the string "NA" . How would you make full_name return NA when either part is missing?
  • What happens if full_name gets vectors of different lengths, and how would you guard against it?