đ 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
đ¯ 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
| Function | Purpose |
|---|---|
| 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
đ Wide Format vs Long Format
| Feature | Wide Format | Long Format |
|---|---|---|
| Variables | Spread across multiple columns. | Stored in fewer columns. |
| Rows | Fewer rows. | More rows. |
| Best For | Reports and spreadsheets. | Analysis and visualization. |
| Main Functions | pivot_wider() | pivot_longer() |
â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| 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 keys | May 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
đ 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.