Wide to Long Format
Lesson 9 of 14 in Coddy's Data Manipulation in R course.
In this lesson, we'll focus on using the pivot_longer() function from the tidyr package to convert data from wide to long format.
Understanding Wide and Long Formats
Wide format: Each variable has its own column.
Long format: Variables are stored in key-value pairs, with one column for the variable names and another for the values.
The pivot_longer() Function
The pivot_longer() function from tidyr is used to transform data from wide to long format. Here's its basic syntax:
pivot_longer(data, cols, names_to = "key", values_to = "value")Example:
# Load required library
library(tidyr)
# Create a sample wide format dataset
wide_data <- data.frame(
name = c("Alice", "Bob", "Charlie"),
math = c(85, 76, 90),
science = c(92, 88, 75),
history = c(78, 82, 85)
)
# Convert to long format
long_data <- pivot_longer(wide_data,
cols = c(math, science, history),
names_to = "subject",
values_to = "score")
print(long_data)Output:
# A tibble: 9 × 3
name subject score
<chr> <chr> <dbl>
1 Alice math 85
2 Alice science 92
3 Alice history 78
4 Bob math 76
5 Bob science 88
6 Bob history 82
7 Charlie math 90
8 Charlie science 75
9 Charlie history 85Instead of having 4 columns: name, math, science and history we have 3 columns: name, subject, score
Understanding the Arguments
cols: Specifies which columns to pivot into longer format.names_to: Names the new column that will contain the column names from the wide format.values_to: Names the new column that will contain the values.
Importance in Data Analysis
Long format data is often preferred because it's:
- Easier to manipulate and analyze with tools like
dplyr - More suitable for statistical modeling and visualization
- Consistent with the principles of tidy data
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a function that transforms a wide-format dataset of student exam scores into a long format using the pivot_longer() function from the tidyr package. The function should perform the following operations:
- Convert the input string into a data frame
- Use
pivot_longer()to transform the data from wide to long format - Rename the resulting columns to "subject" and "score"
- Arrange the data by student name and then by subject
- Print the resulting long-format data frame
Try it yourself
# Read input
con <- file("stdin", "r")
input_string <- suppressWarnings(readLines(con))
# Convert input string to data frame
data <- read.csv(text = input_string, stringsAsFactors = FALSE)
# Load required package
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(dplyr))
# TODO: Write your code below to transform the data
# Use pivot_longer() to transform from wide to long format
# Rename columns to "subject" and "score"
# Arrange the data by student name and then by subject
# Print the resulting long-format data frame
print(result)