📖 Scenario: Imagine you work in a small bakery. You keep track of daily sales for different types of bread. The data is messy and hard to understand. You want to tidy it so you can easily find out which bread sells best each day.
🎯 Goal: You will create a small messy data frame, then tidy it step-by-step using R. This will help you see why tidy data makes analysis easier and faster.
📋 What You'll Learn
Create a data frame called sales with messy sales data
Create a vector called days with day names
Use tidyr::pivot_longer() to tidy the data into a long format
Print the tidy data frame called tidy_sales
💡 Why This Matters
🌍 Real World
Tidy data helps businesses quickly analyze sales trends by day or product.
💼 Career
Data analysts and scientists use tidy data to prepare datasets for clear and efficient analysis.
Progress0 / 4 steps
1
Create messy sales data
Create a data frame called sales with columns bread, Monday, and Tuesday. Use these exact values: bread has "Sourdough", "Baguette", "Rye"; Monday has 10, 15, 7; Tuesday has 12, 18, 5.
R Programming
Hint
Use data.frame() with named vectors for each column.
2
Create a vector of days
Create a character vector called days with the exact values "Monday" and "Tuesday".
R Programming
Hint
Use c() to create a vector with the two day names.
3
Tidy the sales data
Use tidyr::pivot_longer() on sales to create a new data frame called tidy_sales. Pivot the columns Monday and Tuesday into two columns: day and sales. Use names_to = "day" and values_to = "sales".
R Programming
Hint
Use pivot_longer() with cols = days to select the day columns.
4
Print the tidy data
Print the tidy_sales data frame to see the tidy data format.
R Programming
Hint
Use print(tidy_sales) to show the tidy data frame.
Practice
(1/5)
1. What is the main reason tidy data makes analysis easier in R?
easy
A. Data is stored in a single file format
B. Data is always numeric
C. Each variable is a column and each observation is a row
D. Data is sorted alphabetically
Solution
Step 1: Understand tidy data structure
Tidy data means each variable is stored in its own column and each observation in its own row.
Step 2: Connect tidy data to analysis ease
This structure makes it easier for R functions and packages to process and analyze data efficiently.
Final Answer:
Each variable is a column and each observation is a row -> Option C
Quick Check:
Tidy data = variable as column, observation as row [OK]
Hint: Remember: tidy means variables in columns, observations in rows [OK]
Common Mistakes:
Thinking tidy data means data is sorted
Assuming tidy data must be numeric only
Confusing file format with data structure
2. Which of the following is the correct way to represent tidy data in R?
easy
A. Each column is a variable, each row is an observation
B. Data stored as a list of vectors without names
C. Each row is a variable, each column is an observation
D. Data stored as a single vector with mixed types
Solution
Step 1: Recall tidy data format
Tidy data requires each column to represent a variable and each row to represent an observation.
Step 2: Match options to tidy data
Each column is a variable, each row is an observation correctly states this format, while others do not follow tidy data principles.
Final Answer:
Each column is a variable, each row is an observation -> Option A
Quick Check:
Columns = variables, rows = observations [OK]
Hint: Variables go in columns, observations go in rows [OK]
Common Mistakes:
Swapping rows and columns roles
Using unnamed lists instead of data frames
Mixing data types in a single vector
3. Given this R data frame:
df <- data.frame(
id = c(1, 2),
age = c(30, 25),
height = c(170, 160)
)
Which statement about this data is true?
medium
A. It is tidy because each variable is a column and each observation is a row
B. It is not tidy because variables are mixed in rows
C. It is not tidy because data types differ in columns
D. It is tidy because all data is numeric
Solution
Step 1: Examine data frame structure
The data frame has columns named id, age, and height, each representing a variable.
Step 2: Check observations in rows
Each row represents one observation with values for all variables.
Final Answer:
It is tidy because each variable is a column and each observation is a row -> Option A
Quick Check:
Variables in columns and observations in rows = tidy [OK]
Hint: Check if columns are variables and rows are observations [OK]