Rを用いた実用的なデータクリーニング:tidyr、dplyr、stringrを活用した包括的なワークフロー
これまでの28回のレッスンでは「分析」について学んできましたが、実際のプロジェクトでは、時間の80%がデータクレンジングに費やされます。マネージャーから提供されるCSVファイルには、常に欠損値、外れ値、重複、不適切なデータ型が含まれています。このレッスンでは、Rを使って「実世界の不純なデータ」を徹底的にクレンジングしていきます。
このレッスンを修了すると、あらゆるCSVファイルのクリーニングができるようになります。欠損値、外れ値、重複データ、不適切なデータ型、非標準の文字列、日付形式などに対応し、分析への道筋を整えることができます。
1. 学習内容
- データクレンジングにおける4つの主要な課題(欠損値、外れ値、重複、データ型)
- 欠損値の処理(drop_na/fill/replace)
- 外れ値の処理(IQR/3σ/置換)
- 重複する値(一意)
- 型変換(as.numeric/as.Date)
- 弦のクリーニング(
stringrの完全ガイド) - 日付と時刻の処理 (lubridate)
- janitor パッケージ (clean_names)
- 実践編:1,000行の不正データをクリーンアップする
2. 不完全なデータにまつわる現実的な課題
(1) 課題:マネージャーから提供された「膨大なデータ」
アリスは上司から「Customer Data.csv」という名前のファイルを受け取りました。そのファイルを開くと、次のような内容が表示されました:
id ,Name ,Cell phone number ,Email ,Date of Registration
001 ,Alice ,138-0000-1234 ,ZHANGSAN@163.COM ,2024/01/15
002 ,Bob ,+86 139 0000 5678, lisi@example.com ,2024.01.20
003 , Alice ,13800001111 , ,2024-02-01
004 ,Charlie ,138-0000-2222 ,wangwu@gmail.com ,2024-02-05
005 ,Charlie ,138-0000-2222 ,wangwu@gmail.com ,2024-02-05
006 ,Eve ,(139)0000-3333 ,qianqi@ qq.com ,2024-13-45
6つの主な問題点:① IDの形式 ② スペースを含む名前 ③ 携帯電話番号の形式が統一されていない ④ 大文字と小文字が混在し、スペースを含むメールアドレス ⑤ 値の欠落 ⑥ 重複行 ⑦ 日付の形式が統一されていない ⑧ 無効な日付。
(2) Rを用いた解決策
# Use dplyr + stringr + tidyr + lubridate for cleaning
df_clean <- df |>
janitor::clean_names() |> # Standardization of Listing
mutate(
id = str_pad(id, 3, pad = "0"),
name = str_trim(name),
phone = str_replace_all(phone, "[^0-9]", ""),
email = str_to_lower(str_trim(email))
) |>
drop_na(email) |>
distinct() |>
filter(nchar(phone) == 11) |>
mutate(register_date = parse_date_time(register_date, orders = c("Y/m/d", "Y.m.d", "Y-m-d")))
たった1行のコードで、6つの主要な問題をすべて解決。
3. データクレンジングにおける4つの主要な課題
(1) 4つの主要課題の概要
graph TB
A[Dirty Data] --> B[Missing values Missing]
A --> C[Outliers Outliers]
A --> D[Duplicate values Duplicates]
A --> E[Type error Type]
style A fill:#fff3cd
style B fill:#cce5ff
style C fill:#d4edda
style D fill:#f8d7da
style E fill:#e1d4ff
4. 欠損値の取り扱い
(1) 欠損値の特定
library(tidyr)
# Count the number of missing values in each column
df |> summarise(across(everything(), ~ sum(is.na(.))))
# Missing Value Rate
df |> summarise(across(everything(), ~ mean(is.na(.)) * 100))
# Missing-value patterns
naniar::vis_miss(df)
(2) 3つの経営戦略
| 戦略 | 適用範囲 | R関数 |
|---|---|---|
| 削除 | 欠損値(5%未満)、ランダムな欠損値 | drop_na() |
| 入力 | 多くの項目が欠落しており、削除できません | replace_na() |
| フィールド | 欠損値には意味がある(「メールアドレスなし」=空白) | is_missing 列を追加 |
# 1. Delete: rows containing NA
df |> drop_na()
# 2. Delete: Specified Column
df |> drop_na(email, phone)
# 3. Fill: Fixed value
df |> replace_na(list(email = "unknown@example.com", phone = "Not provided"))
# 4. Fill: Mean/Median
df |>
mutate(
age = replace_na(age, mean(age, na.rm = TRUE)),
income = replace_na(income, median(income, na.rm = TRUE))
)
# 5. Mark
df |>
mutate(email_missing = is.na(email))
(3) 上級編:fill() を使って「前」と「後」を埋める
# Time Series: Missing values are imputed with the preceding value
df |> fill(value, .direction = "down") # Use the previous line
df |> fill(value, .direction = "up") # Use the next line
df |> fill(value, .direction = "downup") # Two-way
5. 外れ値の取り扱い
(1) 3つの識別方法
# 1. IQR method (Robust, Recommended)
is_outlier_iqr <- function(x) {
q1 <- quantile(x, 0.25, na.rm = TRUE)
q3 <- quantile(x, 0.75, na.rm = TRUE)
iqr <- q3 - q1
x < q1 - 1.5 * iqr | x > q3 + 1.5 * iqr
}
# 2. 3-sigma method (Normal only)
is_outlier_z <- function(x, threshold = 3) {
z <- (x - mean(x, na.rm = TRUE)) / sd(x, na.rm = TRUE)
abs(z) > threshold
}
# 3. Business Rules (Such as age < 0 or > 150)
df |> filter(age < 0 | age > 150)
(2) 4つの治療法
# 1. Delete
df |> filter(!is_outlier_iqr(income))
# 2. Replace with NA (Leave it for further analysis and processing)
df |> mutate(income = ifelse(is_outlier_iqr(income), NA, income))
# 3. Replace with the median (Robust)
df |>
mutate(income = ifelse(is_outlier_iqr(income),
median(income, na.rm = TRUE), income))
# 4. Winsorize (Truncate to 5% / 95% quantile)
df |>
mutate(income = DescTools::Winsorize(income, probs = c(0.05, 0.95)))
6. 重複値の処理
# 1. Exactly duplicate rows
df |> distinct() # Duplicate Removal
sum(duplicated(df)) # Count
# 2. Remove duplicates based on the specified column
df |> distinct(name, phone, .keep_all = TRUE)
# 3. Mark as Duplicate
df |> mutate(is_dup = duplicated(.))
# 4. Find Duplicates
df |> filter(duplicated(df) | duplicated(df, fromLast = TRUE))
7. 型変換
(1) 数値
# Character -> Numeric (Processing "$1,000" Format)
df |>
mutate(price = as.numeric(str_replace_all(price, "[$,]", "")))
# Processing Percentage "15%" → 0.15
df |>
mutate(rate = as.numeric(str_replace(rate, "%", "")) / 100)
# Processing NA Conversion Error
df |>
mutate(value = as.numeric(value), # Failure leads to change NA + Warning
bad = is.na(value) & !is.na(original_value))
(2) 日付型
library(lubridate)
# Automatic Parsing of Multiple Date Formats
df |>
mutate(date = parse_date_time(date,
orders = c("Y/m/d", "Y-m-d", "Y.m.d")))
# Extract Year, Month, and Day
df |>
mutate(
year = year(date),
month = month(date),
day = day(date),
weekday = wday(date, label = TRUE)
)
# Handling Invalid Dates
df |>
mutate(
date_parsed = parse_date_time(date, orders = "Y-m-d"),
is_invalid = is.na(date_parsed) & !is.na(date)
) |>
filter(!is_invalid)
(3) 因子タイプ
# Character -> factor (In the specified order)
df |>
mutate(grade = factor(grade, levels = c("Poor", "Mid", "Good", "Excellent"),
ordered = TRUE))
# Numeric -> factor (Bin Sorting)
df |>
mutate(age_group = cut(age,
breaks = c(0, 18, 35, 60, 100),
labels = c("Boy", "Youth", "Middle Age", "Old Age")))
8. 文字列のクリーニング (stringr)
library(stringr)
df |>
mutate(
# Remove spaces
name = str_trim(name),
# Uppercase and lowercase
email = str_to_lower(email),
# Remove Special Characters
phone = str_replace_all(phone, "[^0-9]", ""),
# Validation Format
email_valid = str_detect(email, "^[\\w.+-]+@[\\w.-]+\\.[a-z]+$"),
# Extract
phone_prefix = str_sub(phone, 1, 3)
)
9. janitor パッケージ(清掃の強力ツール)
install.packages("janitor")
library(janitor)
# 1. Standardized column names (lowercase + underscore + remove special characters)
df |> clean_names()
# "First Name" → "first_name"
# "Age(yrs)" → "age"
# 2. Duplicate Removal + Report
df |> get_dupes() # Show duplicate rows
# 3. Remove blank lines/col
df |> remove_empty(c("rows", "cols"))
# 4. Consistency Check
df |> tabyl(category) # Similar table
10. 完全な例:1,000行の不正データをクリーンアップする
以下は、このレッスンで取り上げたすべてのクリーニングの概念を結びつけた完全なワークフローの例です。
▶ サンプル:1,000件の顧客の不正データの完全なクリーニング
# ============================================
# 1000 Complete Cleaning of Dirty Customer Data
# Features: All 6 major issues resolved
# ============================================
library(dplyr)
library(tidyr)
library(stringr)
library(lubridate)
library(janitor)
# 1. Constructing Dirty Data
set.seed(42)
n <- 1000
df_raw <- tibble(
ID = sprintf("%04d", 1:n),
Customer Name = paste0(" ", c("Alice", "Bob", "Charlie", "Diana", "Eve")[sample(5, n, TRUE)], " "),
Cell Phone = sample(c("138-0000-1234", "+86 139 0000 5678", "(13900001111)",
"138.0000.2222", "13900003333", "13900009999XX", NA), n, TRUE),
Email = sample(c("user@163.COM", "lisi@example.com ",
"wangwu@gmail.com", "zhaoliu@ qq.com",
NA, ""), n, TRUE),
Date of Registration = sample(c("2024/01/15", "2024.01.20", "2024-02-01",
"2024-13-45", NA), n, TRUE),
Age = sample(c(20:80, -5, 200, NA), n, TRUE)
)
# Add some repetition
df_raw <- bind_rows(df_raw, df_raw[1:50, ])
cat("=== Statistics on Raw Data Issues ===\n")
cat("Number of lines:", nrow(df_raw), "(includes 50 duplicate rows)\n")
cat("Email address missing:", sum(is.na(df_raw$Email) | df_raw$Email == ""), "rows\n")
cat("Missing Cell Phone:", sum(is.na(df_raw$Cell Phone)), "rows\n")
cat("Date Missing:", sum(is.na(df_raw$Date of Registration)), "rows\n")
cat("Age Anomaly (< 0 or > 150):",
sum(df_raw$Age < 0 | df_raw$Age > 150, na.rm = TRUE), "rows\n\n")
# 2. Complete Cleaning Process
df_clean <- df_raw |>
# 2.1 Standardized Listing
clean_names() |>
# 2.2 Remove duplicates
distinct() |>
# 2.3 Clean Name
mutate(Customer Name = str_trim(Customer Name)) |>
# 2.4 Clear Mobile Phone Number
mutate(
Cell Phone_Pure numbers = str_replace_all(Cell Phone, "[^0-9]", ""), # Remove all non-numeric characters
Cell Phone_Valid = nchar(Cell Phone_Pure numbers) == 11 & str_detect(Cell Phone_Pure numbers, "^1[3-9]")
) |>
# 2.5 Clean the Mailbox
mutate(
Email_Cleaning = str_trim(Email) |> str_to_lower(),
Email_Valid = str_detect(Email_Cleaning, "^[\\w.+-]+@[\\w.-]+\\.[a-z]+$")
) |>
# 2.6 Handling Missing Values
mutate(
Cell Phone_Pure numbers = ifelse(Cell Phone_Valid, Cell Phone_Pure numbers, NA),
Email_Cleaning = ifelse(Email_Valid, Email_Cleaning, NA)
) |>
# 2.7 Handling Abnormal Ages
mutate(
Age_Cleaning = ifelse(Age < 0 | Age > 150, NA, Age)
) |>
# 2.8 Analysis Date
mutate(
Date of Registration_parsed = parse_date_time(Date of Registration,
orders = c("Y/m/d", "Y.m.d", "Y-m-d")),
Date_Valid = !is.na(Date of Registration_parsed) | is.na(Date of Registration)
) |>
# 2.9 Delete rows where a key field is missing
drop_na(Customer Name, Date of Registration_parsed) |>
# 2.10 Select the last column
select(id, Name = Customer Name, Cell Phone = Cell Phone_Pure numbers, Email = Email_Cleaning,
Age = Age_Cleaning, Date of Registration = Date of Registration_parsed)
cat("=== Data After Cleaning ===\n")
cat("Number of lines:", nrow(df_clean), "\n")
cat("Columns:", ncol(df_clean), "\n")
cat("Email address missing:", sum(is.na(df_clean$Email)), "\n")
cat("Missing Cell Phone:", sum(is.na(df_clean$Cell Phone)), "\n")
cat("Age Missing:", sum(is.na(df_clean$Age)), "\n")
cat("Date Missing:", sum(is.na(df_clean$Date of Registration)), "\n\n")
# 3. Verify the Quality of Cleaning
cat("=== Verify the Quality of Cleaning ===\n")
# 3.1 ID Format
cat("ID Length:", unique(nchar(df_clean$id)), "(Expected 4)\n")
# 3.2 No spaces before or after the name
cat("Name contains leading and trailing spaces:", sum(str_detect(df_clean$Name, "^\\s|\\s$")), "\n")
# 3.3 Mobile Phone Number Format
cat("Cell phone number 11 digits:", sum(nchar(df_clean$Cell Phone, na.rm = TRUE) == 11), "/",
sum(!is.na(df_clean$Cell Phone)), "\n")
# 3.4 Email Format
cat("Email address includes @:", sum(str_detect(df_clean$Email, "@")), "/",
sum(!is.na(df_clean$Email)), "\n")
# 3.5 Age Range
cat("Age Range: [", min(df_clean$Age, na.rm = TRUE), ",",
max(df_clean$Age, na.rm = TRUE), "]\n")
# 4. Output clean data
write_csv(df_clean, "cleaned_data.csv")
cat("\n=== The cleaned data has been saved: cleaned_data.csv ===\n")
# 5. Before and After Cleaning Comparison
cat("\n=== Before and After Cleaning Comparison ===\n")
comparison <- tibble(
Indicators = c("Total Number of Lines", "Columns", "Duplicate Rows", "Email address missing", "Missing Cell Phone", "Age Anomaly"),
Before Cleaning = c(
nrow(df_raw), ncol(df_raw), nrow(df_raw) - nrow(distinct(df_raw)),
sum(is.na(df_raw$Email) | df_raw$Email == ""),
sum(is.na(df_raw$Cell Phone)),
sum(df_raw$Age < 0 | df_raw$Age > 150, na.rm = TRUE)
),
After Cleaning = c(
nrow(df_clean), ncol(df_clean), 0,
sum(is.na(df_clean$Email)),
sum(is.na(df_clean$Cell Phone)),
sum(is.na(df_clean$Age)) # Outliers changed to NA
)
)
print(comparison)
期待される出力(抜粋):
=== Data After Cleaning ===
Number of lines: 923
Columns: 6
Email address missing: 162
Missing Cell Phone: 85
Age Missing: 22
Date Missing: 0
=== Verify the Quality of Cleaning ===
ID Length: 4 (Expected 4)
Name contains leading and trailing spaces: 0
Cell phone number 11 digits: 838 / 838
Email address includes @: 761 / 761
Age Range: [ 20 , 80 ]
❓ よくある質問
clean_names() 列名の標準化 + get_dupes() 重複の検出 + remove_empty() 空の行・列の削除。📖 まとめ
- データクレンジングにおける4つの主要な課題:欠損値/外れ値/重複/データ型
- 欠落:
< 5% delete / 5-20% fill / > 20% delete colおよびnaniar::vis_missの可視化 - 異常値:IQR法(< Q1 - 1.5×IQR または > Q3 + 1.5×IQR);まず、それがエラーなのか、業務上の異常なのかを判断する
- 重複:
distinct()重複の削除 +duplicated()検出 - タイプ:
as.numeric/parse_date_time/factor3点セット - 文字列:トリム + 大文字小文字の変換 + 特殊文字の削除 + 検証 (stringr)
- 日付:
lubridate::parse_date_time()複数の形式を自動的に認識します - 管理人向けパッケージ:
clean_names標準化された名称一覧 +get_dupes+remove_empty - ソート順:リスト → 重複 → 文字列 → 欠落 → 例外 → 型 → 出力
- モデリングを行う前には、必ずクリーニングを行う必要があります。クリーニングをせずにモデリングを行うことはできません。
📝 練習問題
-
基本演習:Rに組み込まれている
airqualityデータセットを用いて、完全なデータクレンジングを行ってください。① 欠損値(平均値または中央値を用いてオゾンの値を補完する)② 重複(airqualityには重複データはありません)③ データ型(すべての列が正しいことを確認する)④ 出力結果を比較する(クレンジング前と後)。 -
基本問題:100行からなる「汚れた」データフレーム(NA値、重複、入力ミス、非標準の文字列を含む)を作成し、
stringr+tidyr+janitorを使用して徹底的にクリーニングを行い、比較表を出力してください。 -
基本演習:
parse_date_timeを使用して、5つの異なる日付形式(「2024-01-15」、「2024/01/15」、「15/01/2024」、「January 15, 2024」、「15-Jan-2024」)を解析し、結果を確認してください。 -
応用演習:
nycflights13::flightsデータセットに対して、完全なデータクレンジングを実施してください。① 欠損値(dep_time、arr_time、air_time、dep_delay、arr_delay) ② 外れ値(dep_delay > 1440分=24時間) ③ クレンジング前後の統計量を出力してください。 -
課題:1,000行からなる「データモンスター」データセット(4つの主要な問題点をすべて含む)を作成し、このレッスンで学んだツールを使って徹底的にクリーニングを行い、以下のものを出力してください:①クリーニング前後のデータを比較した表、②データクリーニングの品質レポート、③CSV形式のクリーニング済みデータ、④クリーニングプロセスの要約(200文字)。



