Challenge - 5 Problems
Separate and Unite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of separate() with multiple separators
What is the output of this R code using
separate() from tidyr?R Programming
library(tidyr) library(dplyr) df <- tibble(id = 1:2, info = c("a-1_x", "b-2_y")) df %>% separate(info, into = c("letter", "number", "extra"), sep = "[-_]")
Attempts:
2 left
💡 Hint
The separator can be a regular expression matching multiple characters.
✗ Incorrect
The sep = "[-_]" means split at either '-' or '_'. So the string "a-1_x" splits into "a", "1", and "x".
❓ Predict Output
intermediate2:00remaining
Output of unite() with remove = FALSE
What is the output of this R code using
unite() from tidyr?R Programming
library(tidyr) df <- tibble(x = c("a", "b"), y = c(1, 2)) df %>% unite("xy", x, y, sep = ":", remove = FALSE)
Attempts:
2 left
💡 Hint
The argument
remove = FALSE keeps the original columns.✗ Incorrect
The unite() function combines columns x and y into a new column xy separated by ':'. Because remove = FALSE, the original columns x and y remain.
🔧 Debug
advanced2:00remaining
Why does this separate() call fail?
Consider this code snippet:
library(tidyr)
df <- tibble(code = c("A-1", "B-2"))
df %>% separate(code, into = c("letter", "number"), sep = "-")
Why might this code fail if the input data changes to include "C1" (without a dash)?R Programming
library(tidyr) df <- tibble(code = c("A-1", "B-2", "C1")) df %>% separate(code, into = c("letter", "number"), sep = "-")
Attempts:
2 left
💡 Hint
The separate() function expects the separator to be present to split into all requested columns.
✗ Incorrect
If the separator is missing in a string, separate() cannot split it into the requested number of columns and throws an error.
❓ Predict Output
advanced2:00remaining
Result of separate() with extra = 'merge'
What is the output of this R code?
R Programming
library(tidyr) df <- tibble(id = 1, val = "a-b-c") df %>% separate(val, into = c("one", "two"), sep = "-", extra = "merge")
Attempts:
2 left
💡 Hint
The
extra = 'merge' option merges leftover pieces into the last column.✗ Incorrect
Since there are more pieces than columns, extra = 'merge' joins the extra pieces into the last column.
🧠 Conceptual
expert3:00remaining
Understanding unite() with NA values
Given this R code, what is the value of the 'combined' column after running unite()?
R Programming
library(tidyr) df <- tibble(a = c("x", NA), b = c("y", "z")) df %>% unite("combined", a, b, sep = "-")
Attempts:
2 left
💡 Hint
unite() uses paste(collapse = sep) internally, which converts NA to the string "NA".
✗ Incorrect
The unite() function combines columns using paste(collapse = "-"), which converts NA to the string "NA", resulting in "x-y" and "NA-z".