0
0
R Programmingprogramming~20 mins

Nesting and unnesting in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nesting and Unnesting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of nested list unnesting with dplyr
What is the output of the following R code using dplyr and tidyr to unnest a nested list column?
R Programming
library(dplyr)
library(tidyr)
df <- tibble(id = 1:2, data = list(tibble(x = 1:2), tibble(x = 3:4)))
unnest(df, data)
AA tibble with 2 rows: id 1 and 2, x column with list of vectors
BA tibble with 2 rows: id 1 and 2, data column still nested
CError: object 'unnest' not found
DA tibble with 4 rows: id repeated twice for each nested tibble, x values 1,2,3,4
Attempts:
2 left
💡 Hint
Think about how unnest() expands nested tibbles into rows.
Predict Output
intermediate
2:00remaining
Result of nesting and unnesting with multiple columns
Given this R code, what is the output after nesting and then unnesting?
R Programming
library(dplyr)
library(tidyr)
df <- tibble(group = c('A', 'A', 'B'), value = 1:3)
nested <- df %>% group_by(group) %>% nest()
unnest(nested, data)
ADataframe with 3 rows, group column repeated, value column expanded
BDataframe with 2 rows, group column and nested data column
COriginal dataframe with columns group and value, 3 rows
DError: object 'nest' not found
Attempts:
2 left
💡 Hint
Nesting groups rows, unnesting expands them back.
🔧 Debug
advanced
2:00remaining
Identify the error in unnesting a list-column
What error does this R code produce and why?
R Programming
library(tidyr)
df <- tibble(id = 1:2, vals = list(1:3, 4:5))
unnest(df, vals, keep_empty = TRUE)
AError: object 'unnest' not found
BNo error, returns 5 rows with id repeated accordingly
CError: unused argument (keep_empty = TRUE)
DError: vals must be a data frame or list of data frames
Attempts:
2 left
💡 Hint
Check the arguments supported by the current unnest() function version.
📝 Syntax
advanced
2:00remaining
Correct syntax to unnest a nested list-column
Which option correctly unnests the nested list-column data in this tibble?
R Programming
library(tidyr)
df <- tibble(id = 1:2, data = list(tibble(a = 1:2), tibble(a = 3:4)))
Aunnest(df, data = data)
Bunnest(df, cols = data)
Cunnest(df, c(data))
Dunnest(df, data)
Attempts:
2 left
💡 Hint
unnest() uses tidyselect syntax (like select()) to specify columns.
🚀 Application
expert
3:00remaining
How many rows after nesting and unnesting complex data?
Consider this R code that nests and unnests a dataframe with multiple grouping variables and nested tibbles. How many rows does the final dataframe have?
R Programming
library(dplyr)
library(tidyr)
df <- tibble(
  group1 = c('X', 'X', 'Y', 'Y'),
  group2 = c('A', 'B', 'A', 'B'),
  val = 1:4
)
nested <- df %>% group_by(group1) %>% nest()
unnested <- nested %>% unnest(data)
A4 rows, same as original dataframe
B2 rows, one per group1 value
C8 rows, double the original rows
DError due to ambiguous grouping
Attempts:
2 left
💡 Hint
Nesting groups by group1 nests all rows per group1, unnesting expands them back.