0
0
R Programmingprogramming~10 mins

pivot_longer (wide to long) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - pivot_longer (wide to long)
Start with wide data
Select columns to pivot
Gather columns into key-value pairs
Create new 'name' column for keys
Create new 'value' column for values
Return long format data
End
pivot_longer takes wide data and turns selected columns into two columns: one for the old column names (keys) and one for their values, making data longer.
Execution Sample
R Programming
library(tidyr)
data <- data.frame(
  id = 1:2,
  A = c(10, 20),
  B = c(30, 40)
)
pivot_longer(data, cols = c(A, B), names_to = "variable", values_to = "value")
This code converts columns A and B into a longer format with 'variable' and 'value' columns.
Execution Table
StepActionInput DataOutput DataNotes
1Start with wide dataid | A | B 1 |10|30 2 |20|40Same as inputInitial wide data with columns id, A, B
2Select columns to pivotColumns A and BColumns A and B selectedChoosing which columns to make longer
3Gather columns into key-value pairsA=10, B=30 (row 1)variable='A', value=10 variable='B', value=30Row 1 split into two rows
4Gather columns into key-value pairsA=20, B=40 (row 2)variable='A', value=20 variable='B', value=40Row 2 split into two rows
5Add id column to each new rowid=1 or 2id=1, variable='A', value=10 id=1, variable='B', value=30 id=2, variable='A', value=20 id=2, variable='B', value=40id repeats for each new row
6Return long format dataAll rows combinedid | variable | value 1 | A | 10 1 | B | 30 2 | A | 20 2 | B | 40Final long format data
💡 All selected columns pivoted; data transformed from wide to long format.
Variable Tracker
VariableStartAfter Step 3After Step 5Final
datadata.frame with columns id, A, Bid column unchanged; A and B split into key-value pairsid repeated for each key-value pairlong format data with id, variable, value columns
Key Moments - 3 Insights
Why do the number of rows increase after pivot_longer?
Because each selected column in the wide data becomes its own row in the long data, as shown in execution_table steps 3 and 4 where one row splits into multiple rows.
What do the 'names_to' and 'values_to' arguments do?
'names_to' creates the new column holding the old column names (keys), and 'values_to' creates the new column holding the values, as seen in the output data in execution_table step 6.
Does the original id column get changed or removed?
No, the id column stays the same and repeats for each new row created, as shown in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What happens to the row with id=1?
AIt stays as one row with combined values.
BIt is removed from the data.
CIt splits into two rows with variable 'A' and 'B'.
DIt duplicates the id column.
💡 Hint
Check the 'Output Data' column in step 3 of execution_table.
At which step does the id column get repeated for each new row?
AStep 4
BStep 5
CStep 2
DStep 6
💡 Hint
Look at the 'Notes' column describing id repetition in execution_table.
If you only pivot column A, how would the final number of rows change compared to pivoting A and B?
AIt would be half the number of rows.
BIt would be double the number of rows.
CIt would be the same number of rows.
DIt would be zero rows.
💡 Hint
Refer to variable_tracker and how each selected column creates new rows.
Concept Snapshot
pivot_longer(data, cols, names_to, values_to)
- Converts wide data to long format
- 'cols' selects columns to pivot
- 'names_to' names new key column
- 'values_to' names new value column
- Rows increase as columns become rows
Full Transcript
pivot_longer is a function in R that changes data from wide format to long format. It takes selected columns and turns them into two new columns: one for the old column names and one for their values. This makes the data longer by increasing the number of rows. The original columns not selected stay the same and repeat for each new row. This process helps when you want to analyze or plot data more easily. The execution steps show how each row splits into multiple rows with new key-value pairs, and the id column repeats accordingly.