Menu
Coddy logo textTech

Long to Wide Format

Lesson 10 of 14 in Coddy's Data Manipulation in R course.

Converting data from long to wide format is an essential skill in data manipulation, particularly useful for creating more compact representations of data. In this lesson, we'll focus on using the pivot_wider() function from the tidyr package to transform data from long to wide format.

1. Understanding Long and Wide Formats

Long format: Variables are stored in key-value pairs, with one column for the variable names and another for the values.
Wide format: Each variable has its own column.

2. The pivot_wider() Function

The pivot_wider() function from tidyr is used to transform data from long to wide format. Here's its basic syntax:

pivot_wider(data, names_from = "key", values_from = "value")

3. Example: Using pivot_wider()

Let's look at an example:


# Load required library
library(tidyr)

# Create a sample long format dataset
long_data <- data.frame(
  name = c("Alice", "Alice", "Alice", "Bob", "Bob", "Bob"),
  subject = c("math", "science", "history", "math", "science", "history"),
  score = c(85, 92, 78, 76, 88, 82)
)

# Convert to wide format
wide_data <- pivot_wider(long_data, 
                         names_from = "subject",
                         values_from = "score")

print(wide_data)

4. Understanding the Arguments

  • names_from: Specifies which column contains the names for the new columns in the wide format.
  • values_from: Specifies which column contains the values for the new columns.

5. Handling Multiple Value Columns

You can pivot multiple value columns simultaneously:


long_data_multi <- data.frame(
  name = c("Alice", "Alice", "Bob", "Bob"),
  year = c(2022, 2023, 2022, 2023),
  score = c(85, 90, 76, 82),
  rank = c(2, 1, 3, 2)
)

wide_data_multi <- pivot_wider(long_data_multi,
                               names_from = year,
                               values_from = c(score, rank))

print(wide_data_multi)

By mastering the pivot_wider() function, you'll be able to efficiently reshape your data from long to wide format, creating more compact representations for specific analysis needs in R.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

Create a function that transforms a long-format dataset of student grades into a wide format using the pivot_wider() function from the tidyr package. The function should perform the following operations:

  1. Convert the input string into a data frame
  2. Use pivot_wider() to transform the data from long to wide format
  3. Arrange the resulting data frame by student name (use %>% arrange(student))
  4. Print the wide-format data frame

Try it yourself

# Read input
con <- file("stdin", "r")
input_string <- suppressWarnings(readLines(con))

# Convert input string to data frame
df <- read.csv(text = input_string, stringsAsFactors = FALSE)

# Load required library
suppressPackageStartupMessages(library(tidyr))
suppressPackageStartupMessages(library(dplyr))

# TODO: Write your code below to transform the data frame
# Hint: Use pivot_wider() function to transform from long to wide format
# Don't forget to arrange the resulting data frame by student name

# Print the result
# print(result)

All lessons in Data Manipulation in R