0
0
R-programmingHow-ToBeginner · 4 min read

How to Use tidyr in R for Data Tidying

Use the tidyr package in R to reshape and tidy data frames with functions like pivot_longer() to make data longer and pivot_wider() to make data wider. These functions help organize data into a clean, easy-to-analyze format.
📐

Syntax

pivot_longer() converts wide data to long format by gathering columns into key-value pairs.

pivot_wider() converts long data to wide format by spreading key-value pairs into columns.

Both functions take a data frame and specify which columns to reshape.

r
pivot_longer(data, cols, names_to = "name", values_to = "value")
pivot_wider(data, names_from = "name", values_from = "value")
💻

Example

This example shows how to use pivot_longer() to turn wide data into long format, then use pivot_wider() to go back to wide format.

r
library(tidyr)
library(dplyr)

# Sample wide data frame
wide_data <- tibble(
  id = 1:2,
  treatment_a = c(5, 3),
  treatment_b = c(2, 4)
)

# Convert wide to long format
long_data <- pivot_longer(wide_data, cols = c(treatment_a, treatment_b),
                          names_to = "treatment", values_to = "result")

# Convert back to wide format
wide_again <- pivot_wider(long_data, names_from = treatment, values_from = result)

list(long_data = long_data, wide_again = wide_again)
Output
long_data # A tibble: 4 × 3 id treatment result <int> <chr> <dbl> 1 1 treatment_a 5 2 1 treatment_b 2 3 2 treatment_a 3 4 2 treatment_b 4 wide_again # A tibble: 2 × 3 id treatment_a treatment_b <int> <dbl> <dbl> 1 1 5 2 2 2 3 4
⚠️

Common Pitfalls

  • Not specifying the correct columns in cols for pivot_longer() can cause unexpected results.
  • Using pivot_wider() without unique keys can create duplicate rows or errors.
  • Forgetting to load tidyr with library(tidyr) causes function not found errors.
r
library(tidyr)

# Wrong: missing cols argument
# pivot_longer(wide_data)

# Right: specify columns to gather
pivot_longer(wide_data, cols = c(treatment_a, treatment_b))
📊

Quick Reference

FunctionPurposeKey Arguments
pivot_longer()Make data longer by gathering columnsdata, cols, names_to, values_to
pivot_wider()Make data wider by spreading key-value pairsdata, names_from, values_from
separate()Split one column into multiple columnsdata, col, into, sep
unite()Combine multiple columns into onedata, col, ..., sep

Key Takeaways

Use tidyr's pivot_longer() to reshape wide data into a longer, tidy format.
Use pivot_wider() to spread long data back into wide format.
Always specify the correct columns to reshape to avoid errors.
Load tidyr with library(tidyr) before using its functions.
tidyr helps make data easier to analyze by organizing it cleanly.