Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does nest() do in R when applied to a grouped data frame?
easy
A. It sorts the data frame by a column.
B. It separates a list-column into multiple rows.
C. It removes grouping from the data frame.
D. It groups specified columns into a list-column inside the data frame.

Solution

  1. Step 1: Understand the purpose of nest()

    The nest() function groups specified columns into a list-column, keeping other columns as keys.
  2. Step 2: Identify the effect on data frame structure

    This creates a nested data frame where each row contains a key and a list of grouped data.
  3. Final Answer:

    It groups specified columns into a list-column inside the data frame. -> Option D
  4. Quick Check:

    nest() = groups columns into list-column [OK]
Hint: nest() groups columns into list-columns quickly [OK]
Common Mistakes:
  • Confusing nest() with unnest()
  • Thinking nest() sorts data
  • Assuming nest() removes grouping
2. Which of the following is the correct syntax to unnest a list-column named data in a data frame df using tidyr?
easy
A. unnest(df, cols)
B. df %>% unnest()
C. df %>% unnest(cols = data)
D. unnest(df)

Solution

  1. Step 1: Recall unnest() syntax in tidyr

    The correct syntax uses unnest() with the argument cols = specifying the list-column to expand.
  2. Step 2: Identify the pipe-friendly syntax

    Using the pipe operator, the syntax is df %>% unnest(cols = data), which is the modern recommended style.
  3. Final Answer:

    df %>% unnest(cols = data) -> Option C
  4. Quick Check:

    unnest() needs cols= and pipe for data frame [OK]
Hint: Use pipe and cols= to unnest list-columns [OK]
Common Mistakes:
  • Omitting cols= argument
  • Passing data frame as first argument without pipe
  • Using unnest(df, data) without cols=
3. Given the code:
library(tidyr)
library(dplyr)
df <- tibble(group = c('A', 'A', 'B', 'B'), value = 1:4)
nested_df <- df %>% group_by(group) %>% nest()
unnested_df <- nested_df %>% unnest(cols = data)

What will be the output of unnested_df?
medium
A. A data frame identical to the original df with columns group and value.
B. A data frame with columns group and a list-column data.
C. An error because unnest() cannot be applied here.
D. A data frame with only the group column.

Solution

  1. Step 1: Understand the nesting step

    The nest() groups rows by group and stores the remaining columns in a list-column named data.
  2. Step 2: Understand the unnesting step

    The unnest() expands the list-column data back into regular rows, restoring the original structure.
  3. Final Answer:

    A data frame identical to the original df with columns group and value. -> Option A
  4. Quick Check:

    nest() then unnest() returns original data [OK]
Hint: nest() then unnest() restores original data frame [OK]
Common Mistakes:
  • Thinking unnest() returns list-column
  • Expecting error from unnest()
  • Assuming columns are dropped
4. Consider this code snippet:
library(tidyr)
df <- tibble(id = 1:2, data = list(tibble(x = 1:2), tibble(x = 3:4)))
unnested <- unnest(df)

It throws an error. What is the likely cause?
medium
A. The list-column data contains data frames, which cannot be unnested.
B. The unnest() function requires the cols argument to specify the list-column.
C. The tibble package is not loaded.
D. The unnest() function cannot unnest list-columns with numeric data.

Solution

  1. Step 1: Check unnest() argument requirements

    Modern unnest() requires specifying which columns to unnest using the cols = argument.
  2. Step 2: Identify missing argument causing error

    Omitting cols = data causes the function to fail because it doesn't know which list-column to expand.
  3. Final Answer:

    The unnest() function requires the cols argument to specify the list-column. -> Option B
  4. Quick Check:

    unnest() needs cols= argument [OK]
Hint: Always specify cols= when using unnest() [OK]
Common Mistakes:
  • Forgetting cols= argument
  • Assuming unnest() works without specifying columns
  • Confusing list-column content type
5. You have a data frame df with columns category, subcat, and value. You want to nest subcat and value by category, then filter out groups where the sum of value is less than 10, and finally unnest the filtered data. Which sequence of commands correctly achieves this?
hard
A. df %>% group_by(category) %>% nest(data = c(subcat, value)) %>% filter(map_dbl(data, ~sum(.x$value)) >= 10) %>% unnest(cols = data)
B. df %>% nest(data = c(subcat, value)) %>% filter(sum(value) >= 10) %>% unnest(data)
C. df %>% group_by(category) %>% nest(cols = c(subcat, value)) %>% filter(sum(value) >= 10) %>% unnest(cols = data)
D. df %>% group_by(category) %>% nest(data = c(subcat, value)) %>% filter(sum(data$value) >= 10) %>% unnest(cols = data)

Solution

  1. Step 1: Nest subcat and value by category

    Use group_by(category) then nest(data = c(subcat, value)) to create a list-column named data with those columns.
  2. Step 2: Filter groups by sum of value

    Use filter(map_dbl(data, ~sum(.x$value)) >= 10) to keep only groups where sum of value in nested data is at least 10.
  3. Step 3: Unnest the filtered data

    Finally, use unnest(cols = data) to expand the filtered nested data back into rows.
  4. Final Answer:

    df %>% group_by(category) %>% nest(data = c(subcat, value)) %>% filter(map_dbl(data, ~sum(.x$value)) >= 10) %>% unnest(cols = data) -> Option A
  5. Quick Check:

    nest(), filter with map_dbl(), then unnest() [OK]
Hint: Use map_dbl() on nested data to filter before unnesting [OK]
Common Mistakes:
  • Filtering before nesting
  • Using sum() directly on list-column
  • Omitting cols= in unnest()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard