0
0
R Programmingprogramming~30 mins

Nesting and unnesting in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Nesting and unnesting in R
📖 Scenario: You work in a small company that keeps track of sales data. The data is grouped by salespeople, and each salesperson has a list of sales with amounts and dates.You want to practice how to group this data into nested lists and then flatten it back to a simple table.
🎯 Goal: You will create a nested data frame with sales grouped by salesperson, then unnest it back to a flat data frame showing all sales.
📋 What You'll Learn
Use a data frame with exact columns: salesperson, amount, date
Create a nested data frame grouped by salesperson
Unnest the nested data frame back to a flat data frame
Print the final unnested data frame
💡 Why This Matters
🌍 Real World
Nesting and unnesting data is useful when you want to group related information together and then expand it back for detailed analysis.
💼 Career
Data analysts and scientists often use nesting to organize complex data and unnesting to prepare data for reports or visualizations.
Progress0 / 4 steps
1
Create the initial sales data frame
Create a data frame called sales_data with these exact rows:
salesperson = c('Alice', 'Alice', 'Bob', 'Bob', 'Charlie'),
amount = c(100, 150, 200, 250, 300),
date = as.Date(c('2024-01-01', '2024-01-05', '2024-01-03', '2024-01-07', '2024-01-10')).
R Programming
Need a hint?

Use data.frame() with the exact column names and values.

2
Load the tidyr package
Write a line to load the tidyr package using library(tidyr).
R Programming
Need a hint?

Use library(tidyr) to load the package.

3
Nest the sales data by salesperson
Create a nested data frame called nested_sales by grouping sales_data by salesperson and nesting the other columns using nest().
R Programming
Need a hint?

Use tidyr::nest() with .by = salesperson and nest amount and date.

4
Unnest the nested sales data and print
Unnest the nested_sales data frame back to a flat data frame called unnested_sales using unnest(). Then print unnested_sales.
R Programming
Need a hint?

Use tidyr::unnest() on nested_sales with data, then print the result.