Data Tidying with tidyr in R

📘 Introduction

Data tidying is the process of organizing data into a clean and consistent structure that is easier to analyze and visualize. The tidyr package, part of the tidyverse, provides simple and powerful functions to reshape, separate, combine, and handle missing data. Tidy data makes analysis more efficient and improves code readability.

Information

In tidy data, each variable occupies one column, each observation occupies one row, and each value occupies one cell.

đŸŽ¯ Why Use tidyr?

  • Organize messy datasets.
  • Prepare data for analysis and visualization.
  • Reshape data between wide and long formats.
  • Handle missing values efficiently.
  • Create consistent and readable datasets.

đŸ“Ļ Installing and Loading tidyr

Install and Load tidyr

install.packages("tidyr")

library(tidyr)

📚 Common tidyr Functions

FunctionPurpose
pivot_longer()Converts wide data to long format.
pivot_wider()Converts long data to wide format.
separate()Splits one column into multiple columns.
unite()Combines multiple columns into one.
drop_na()Removes rows containing missing values.
replace_na()Replaces missing values.
fill()Fills missing values using nearby observations.

📝 Creating a Sample Dataset

Sample Wide Dataset

library(tidyr)

sales <- data.frame(
  Product = c("Laptop", "Tablet"),
  January = c(120, 80),
  February = c(135, 95),
  March = c(150, 110)
)

print(sales)

🔄 Converting Wide Data to Long Format

The pivot_longer() function converts multiple columns into key-value pairs.

Using pivot_longer()

sales %>%
  pivot_longer(
    cols = January:March,
    names_to = "Month",
    values_to = "Sales"
  )

🔁 Converting Long Data to Wide Format

The pivot_wider() function converts long-format data back into a wide table.

Using pivot_wider()

salesLong <- data.frame(
  Product = c(
    "Laptop","Laptop","Laptop",
    "Tablet","Tablet","Tablet"
  ),
  Month = c(
    "January","February","March",
    "January","February","March"
  ),
  Sales = c(
    120,135,150,
    80,95,110
  )
)

salesLong %>%
  pivot_wider(
    names_from = Month,
    values_from = Sales
  )

âœ‚ī¸ Splitting a Column

The separate() function divides a single column into multiple columns.

Using separate()

employees <- data.frame(
  FullName = c(
    "Alice Johnson",
    "Bob Smith"
  )
)

employees %>%
  separate(
    FullName,
    into = c(
      "FirstName",
      "LastName"
    ),
    sep = " "
  )

🔗 Combining Columns

The unite() function combines multiple columns into a single column.

Using unite()

students <- data.frame(
  FirstName = c("Alice","Bob"),
  LastName = c("Johnson","Smith")
)

students %>%
  unite(
    "FullName",
    FirstName,
    LastName,
    sep = " "
  )

❌ Removing Missing Values

The drop_na() function removes rows containing missing values.

Using drop_na()

students <- data.frame(
  Name = c("Alice","Bob","Charlie"),
  Marks = c(85, NA, 90)
)

students %>%
  drop_na()

🔄 Replacing Missing Values

The replace_na() function replaces missing values with specified values.

Using replace_na()

students <- data.frame(
  Name = c("Alice","Bob","Charlie"),
  Marks = c(85, NA, 90)
)

students %>%
  replace_na(
    list(Marks = 0)
  )

âŦ‡ī¸ Filling Missing Values

The fill() function fills missing values using the previous or next non-missing value.

Using fill()

sales <- data.frame(
  Month = c(
    "January",
    NA,
    NA,
    "February",
    NA
  ),
  Sales = c(
    120,
    130,
    140,
    150,
    160
  )
)

sales %>%
  fill(Month)

📊 Checking Data Structure

Before and after tidying data, it is useful to inspect the dataset.

Inspecting Data

str(sales)

head(sales)

summary(sales)

🌍 Real-World Example

A company records quarterly sales in separate columns. The following example converts the data into a long format for easier visualization and analysis.

Quarterly Sales

sales <- data.frame(
  Product = c(
    "Laptop",
    "Tablet"
  ),
  Q1 = c(120,80),
  Q2 = c(135,95),
  Q3 = c(150,110),
  Q4 = c(165,125)
)

sales %>%
  pivot_longer(
    cols = Q1:Q4,
    names_to = "Quarter",
    values_to = "Sales"
  )

🔄 Data Tidying Workflow

Import Data
Inspect Dataset
Tidy Data
Reshape Data
Handle Missing Values
Prepare for Analysis

📋 Wide Format vs Long Format

FeatureWide FormatLong Format
VariablesSpread across multiple columns.Stored in fewer columns.
RowsFewer rows.More rows.
Best ForReports and spreadsheets.Analysis and visualization.
Main Functionspivot_wider()pivot_longer()

âš ī¸ Common Mistakes

MistakeExplanationSolution
Selecting incorrect columns in pivot_longer()Produces an incorrect data structure.Specify only the columns that need reshaping.
Using an incorrect separator in separate()Columns may not split correctly.Verify the delimiter before splitting.
Dropping important rows with drop_na()Useful information may be removed.Review missing values before deleting rows.
Using pivot_wider() with duplicate keysMay generate list columns or warnings.Ensure each key uniquely identifies an observation.

💡 Best Practices

  • Keep data in tidy format whenever possible.
  • Inspect datasets before and after tidying.
  • Use meaningful column names.
  • Handle missing values before performing analysis.
  • Choose long format for most visualization and analysis tasks.

Best Practice

Tidy data is easier to analyze, visualize, and share. The tidyr package provides a consistent set of tools that simplify data cleaning and preparation, making analytical workflows more efficient and reproducible.

📝 Summary

The tidyr package provides powerful tools for organizing and reshaping datasets into tidy formats. You learned how to convert between wide and long formats using pivot_longer() and pivot_wider(), split and combine columns with separate() and unite(), handle missing values using drop_na(), replace_na(), and fill(), and inspect data before analysis. Mastering data tidying is a crucial step toward effective data analysis, visualization, and machine learning in R.

>>"Tidy data transforms messy information into organized knowledge, making every analysis simpler and more meaningful."