0
0
R Programmingprogramming~20 mins

separate and unite in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Separate and Unite Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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 = "[-_]")
AA tibble with columns: id, letter (a-1, b-2), number (x,y), extra (NA, NA)
BError: unused argument sep
CA tibble with columns: id, letter (a, b), number (1_x, 2_y), extra (NA, NA)
DA tibble with columns: id, letter (a,b), number (1,2), extra (x,y)
Attempts:
2 left
💡 Hint
The separator can be a regular expression matching multiple characters.
Predict Output
intermediate
2: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)
AA tibble with columns: x, y, xy (a:1, b:2)
BA tibble with columns: x, y, xy (a1, b2)
CA tibble with columns: xy (a:1, b:2) only
DError: unused argument remove
Attempts:
2 left
💡 Hint
The argument remove = FALSE keeps the original columns.
🔧 Debug
advanced
2: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 = "-")
AIt raises an error because 'C1' does not contain the separator '-' and cannot be split into two parts.
BIt works fine and splits 'C1' into 'C' and '1' automatically.
CIt returns NA for both letter and number for 'C1' without error.
DIt splits 'C1' into 'C1' and NA without error.
Attempts:
2 left
💡 Hint
The separate() function expects the separator to be present to split into all requested columns.
Predict Output
advanced
2: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")
AA tibble with one row: one = 'a', two = 'b'
BA tibble with one row: one = 'a', two = 'b-c'
CA tibble with one row: one = 'a-b', two = 'c'
DError: unused argument extra
Attempts:
2 left
💡 Hint
The extra = 'merge' option merges leftover pieces into the last column.
🧠 Conceptual
expert
3: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 = "-")
Acombined = c("x-y", "-z")
Bcombined = c("x-y", NA)
Ccombined = c("x-y", "NA-z")
Dcombined = c("x-y", "z")
Attempts:
2 left
💡 Hint
unite() uses paste(collapse = sep) internally, which converts NA to the string "NA".