Pivoting Data from Long to Wide Format with pivot_wider
📖 Scenario: Imagine you have survey data collected from different people about their favorite fruits. The data is in a long format, where each row shows one person's choice for one fruit type. You want to reshape this data so that each person has one row, and their favorite fruits are shown in separate columns.
🎯 Goal: You will learn how to use the pivot_wider function from the tidyr package in R to convert data from long format to wide format. This will help you organize data better for analysis or reporting.
📋 What You'll Learn
Create a data frame called
survey_data with specific columns and values.Create a variable called
fruit_column to specify the column to spread.Use
pivot_wider to reshape the data from long to wide format.Print the resulting wide data frame.
💡 Why This Matters
🌍 Real World
Pivoting data from long to wide format is common in data analysis when you want to compare categories side by side, like survey responses or sales data.
💼 Career
Data analysts and scientists often reshape data to prepare it for visualization, modeling, or reporting. Knowing pivot_wider helps you organize data efficiently.
Progress0 / 4 steps