Convert Wide Data to Long Format using pivot_longer
📖 Scenario: You have a small dataset showing sales of different fruits in two months. The data is in a wide format, where each month is a separate column.We want to change this data into a long format, where each row shows the fruit, the month, and the sales amount. This format is easier to analyze and plot.
🎯 Goal: Transform the wide data frame fruit_sales into a long format using pivot_longer from the tidyr package. The result should have columns Fruit, Month, and Sales.
📋 What You'll Learn
Create a data frame called fruit_sales with columns Fruit, Jan, and Feb and the exact data provided.
Create a vector called months containing the column names Jan and Feb.
Use pivot_longer on fruit_sales with cols = months, naming the new columns Month and Sales.
Print the resulting long format data frame.
💡 Why This Matters
🌍 Real World
Data often comes in wide format from spreadsheets or reports. Converting it to long format helps in analysis and visualization.
💼 Career
Data analysts and scientists frequently reshape data using pivot_longer to prepare datasets for plotting and statistical modeling.
Progress0 / 4 steps
1
Create the wide data frame
Create a data frame called fruit_sales with columns Fruit, Jan, and Feb containing these exact rows: 'Apple', 30, 45, 'Banana', 25, 40, 'Cherry', 15, 20.
R Programming
Hint
Use data.frame() with vectors for each column named exactly Fruit, Jan, and Feb.
2
Create a vector of month columns
Create a character vector called months containing the strings 'Jan' and 'Feb'.
R Programming
Hint
Use c() to create a vector with the two month names as strings.
3
Use pivot_longer to reshape the data
Use pivot_longer from the tidyr package on fruit_sales with cols = months, names_to = 'Month', and values_to = 'Sales'. Save the result in a variable called long_sales. Remember to load tidyr first.
R Programming
Hint
Use library(tidyr) to load the package. Then use pivot_longer() with the exact arguments.
4
Print the long format data frame
Write a line to print the variable long_sales.
R Programming
Hint
Use print(long_sales) to display the transformed data.
Practice
(1/5)
1. What does the pivot_longer() function do in R's tidyverse?
easy
A. It filters rows based on a condition.
B. It creates new columns by splitting existing ones.
C. It transforms wide data into long data by stacking columns into rows.
D. It sorts data by a specific column.
Solution
Step 1: Understand the purpose of pivot_longer()
The function is designed to reshape data from wide format (many columns) to long format (fewer columns, more rows).
Step 2: Identify the correct description
Among the options, only It transforms wide data into long data by stacking columns into rows. correctly describes stacking columns into rows, which is what pivot_longer does.
Final Answer:
It transforms wide data into long data by stacking columns into rows. -> Option C
Quick Check:
pivot_longer = wide to long [OK]
Hint: pivot_longer stacks columns into rows to tidy data [OK]
Common Mistakes:
Confusing pivot_longer with filtering or sorting functions
Thinking it splits columns instead of stacking
Assuming it creates new columns instead of reshaping
2. Which of the following is the correct syntax to pivot columns Jan and Feb into longer format with new columns named Month and Sales?
You want to pivot it longer so that the quarter and year are separate columns named Quarter and Year, and the values go into Score. Which code achieves this?