Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
TokyoR#101 パーマーステーションのペンギンたち2 データクリーニング編
Search
bob3bob3
September 16, 2022
Technology
1.1k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TokyoR#101 パーマーステーションのペンギンたち2 データクリーニング編
R言語を用いたデータクリーニング、前処理の話です。
Tokyo.R #101
bob3bob3
September 16, 2022
More Decks by bob3bob3
See All by bob3bob3
RとLLMで自然言語処理
bob3bob3
3
1.1k
RでPSM分析
bob3bob3
1
550
Rでコンジョイント分析 2024年版
bob3bob3
0
2.9k
『改訂新版前処理大全』の話と Apache Parquet の話 #TokyoR
bob3bob3
0
1.5k
R言語の環境構築と基礎 Tokyo.R 112
bob3bob3
0
670
『データ可視化学入門』をPythonからRに翻訳した話(増強版)
bob3bob3
0
620
『データ可視化学入門』を PythonからRに翻訳した話
bob3bob3
1
690
qeMLパッケージの紹介
bob3bob3
0
3k
「国と音楽」 ~spotifyrを用いて~ #muana
bob3bob3
2
690
Other Decks in Technology
See All in Technology
[AWS 秋のクラウドオペレーション祭り 2026]AWS DevOps Agentで変わるリリースと運用対応 ~リリース管理機能と Directed actions のご紹介~
furuton
4
1.1k
パッチ更新でterraform initが落ちた日
grimoh
1
100
AI時代のPRレビューにどう向き合うか?
kairim0
1
110
無理せず続ける、子育てエンジニアの学び
codmoninc
PRO
0
530
プロダクト価値を、 チームが使える判断軸に変える
vivion
0
140
事業活動を AI Ready にする攻めと守りのデータエンジニアリング / data-engineering-for-ai-ready-business
pei0804
6
2.3k
Google Cloud Next Tokyo 26登壇時のスクリプト
recruitengineers
PRO
0
210
初対面のメンバーがチームを組み、Claude Codeでゼロからサービスをリリース
mkdev10
1
110
Snapshot Testing in Practice: Predictable and Reliable SwiftUI Views
fespinoza
0
170
SREでアラート疲れを 解決しよう!
kairim0
1
220
LocalStack を使ったサーバーレスアプリケーション開発 / Serverless Development with LocalStack
kakakakakku
1
340
1万名の社員が使う認証基盤で どう信頼性を担保するか?
kairim0
0
220
Featured
See All Featured
Are puppies a ranking factor?
jonoalderson
2
4k
New Earth Scene 8
popppiees
4
2.7k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
450
Un-Boring Meetings
codingconduct
0
450
Ruling the World: When Life Gets Gamed
codingconduct
0
390
Skip the Path - Find Your Career Trail
mkilby
1
250
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.3k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
700
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
260
The Invisible Side of Design
smashingmag
301
52k
The Curious Case for Waylosing
cassininazir
1
560
Transcript
パーマーステーションの ペンギンたち #2 データクリーニング編 Tokyo.R #101 (2022/09/17) @bob3bob3 Artwork by
@allison_horst
このセッションの位置づけ 中級者セッションのつもりでやっているシリーズです。 1. データセットの紹介(前回) 2. データクリーニング(今回) 3. 探索的データ分析(EDA)と可視化 4. 分析例
◦ シンプソンのパラドクス ◦ 次元縮約 ◦ クラスタリング ◦ 判別モデル ◦ などなど
データクリーニング • データクリーニングとは生のデータを分析しやすい形に整えること。 • データクレンジング、データエディティングとも呼ばれる。 • データ前処理の一部。
パーマーステーションのペンギンたち 前回に引き続き、palmerpenguinsパッ ケージのデータセットを使ってRと戯れま す。 前回資料も参考にしてください。 とりあえず、いますぐ install.packages("palmerpenguins") しましょう! Artwork by
@allison_horst
どんなデータ? • 南極に生息するペンギンの生態調査。2007年、08年、09年の3回の調査。 • 南極のビスコー諸島、ドリーム島、トージャーセン島の3か所。 • アデリーペンギン、ヒゲペンギン、ジェンツーペンギンの3種のペンギン。 • 嘴の長さ、高さ、翼の長さ、体重、性別、血液検査の結果などが含まれる。 アデリー(Adelie)
ジェンツー(Gentoo) ヒゲ(Chinstrap) Stan Shebs (CC BY-SA 3.0) Andrew Shiva(CC BY-SA 4.0) Stan Shebs (CC BY-SA 3.0)
2つのデータセット penguins_raw 生のデータ • 17変数 • studyName: 研究名(越冬隊ごと?) •
Sample Number: 標本番号(連番) • Species: ペンギンの種(学名付き) • Region: 調査した地域 • Island: 調査した島 • Stage: 発育段階 • Individual ID: 個体ID • Clutch Completion: 巣の完成度 • Date Egg: 巣に1個以上の卵が確認された日? • Culmen Length (mm): くちばしの長さ • Culmen Depth (mm): くちばしの高さ • Flipper Length (mm): 翼の長さ • Body Mass (g): 体重 • Sex: 性別 • Delta 15 N (o/oo): 窒素同位体比(δ15N) • Delta 13 C (o/oo): 炭素同位体比(δ13C) • Comments: コメント penguins 使いやすく加工されたデータ • 8変数 • species: ペンギンの種 • island: 調査した島 • bill_length_mm: くちばしの長さ • bill_depth_mm: くちばしの高さ • flipper_length_mm: 翼の長さ • body_mass_g: 体重 • sex: 性別 • year: 調査した年 Artwork by @allison_horst
2つのデータセット penguins_raw 生のデータ • 17変数 • studyName: 研究名(越冬隊ごと?) •
Sample Number: 標本番号(連番) • Species: ペンギンの種(学名付き) • Region: 調査した地域 • Island: 調査した島 • Stage: 発育段階 • Individual ID: 個体ID • Clutch Completion: 巣の完成度 • Date Egg: 巣に1個以上の卵が確認された日? • Culmen Length (mm): くちばしの長さ • Culmen Depth (mm): くちばしの高さ • Flipper Length (mm): 翼の長さ • Body Mass (g): 体重 • Sex: 性別 • Delta 15 N (o/oo): 窒素同位体比(δ15N) • Delta 13 C (o/oo): 炭素同位体比(δ13C) • Comments: コメント penguins 使いやすく加工されたデータ • 8変数 • species: ペンギンの種 • island: 調査した島 • bill_length_mm: くちばしの長さ • bill_depth_mm: くちばしの高さ • flipper_length_mm: 翼の長さ • body_mass_g: 体重 • sex: 性別 • year: 調査した年 Artwork by @allison_horst 今日使うのは こっち。
まずはデータを確認 library(palmerpenguins) #データセット library(summarytools) #サマリー確認用 penguins_raw |> dfSummary() |> view()
palmerpenguinsパッケージ。 今回使うデータセット。 summarytoolsパッケージ。 データの概要を確認するのに非常に便利。
値が1種類しかない。 値が1種類しかない。 種の名前が長い。
変数名に空白や記号 を含む。 三か所に偏ってる。
今回、コメント行は使わ ない。 欠測値が多い。
penguins_rawをキレイに整形する。 1. 分析に不要な列を除外する a. コメント列 b. 値が1つしか無い列 2. 変数名(列名)を調整する a.
小文字に統一 b. 空白を削除 c. 命名規則を統一 d. 単語の置き換え e. 単位の削除 3. 水準名を日本語化する 4. 調査年を追加し、調査日は割愛す る 5. 既存の変数を加工して新たな変数 を追加する 6. NA(欠測値)は特に処理せず残す
完成したスクリプト library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除
各種パッケージの呼び出し library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 tidyverseパッケージ。 モダンなRのためのメタパッケージ。 ggplot2, purrr, tibble, dplyr, tidyr, stringr, readr, forcatsの各パッケージを一度に呼び出す。 janitorパッケージ。 整備されていないデータの確認とクリーニ ングのためのパッケージ。 lubridateパッケージ。 日付のデータをいい感じに処理して くれるパッケージ。
不要な列の削除 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 RegionとStageを削除。 コメント行など分析に使 わない列を削除。
変数名の統一 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 単語をアンダースコアでつなぐ形式 に変換。 キャメルケースなど他の形式に変換 も可能。
変数名の調整 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 rename_with()で関数を使った規則 的な変数名の変換ができる。
水準の調整 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 変数名を空白で分割し、最初の文字列を残す。
水準の調整 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除
水準の調整 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除 year()で日付から年だけを取り出す。
変数の加工と追加 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除
不要になった変数を削除 library(tidyverse) # データ処理全般 library(palmerpenguins) # データセット library(janitor) # データクリーニング用
library(lubridate) # 日時データ処理用 penguins_edit <- penguins_raw |> remove_constant() |> # すべて同じ値の列を削除 select(!c(studyName, `Sample Number`, `Clutch Completion`, Comments)) |> # 不要列削除 clean_names() |> # 命名規則をスネークケースに統一 rename_with(~ str_replace(., "culmen", "bill")) |> # 単語の置き換え rename_with(~ str_remove(., "_mm|_g|_o_oo")) |> # 単位の削除 rename_with(~ str_remove_all(., "_"), starts_with("delta")) |> # deltaの_を削除 mutate( species = species |> str_split(" ") |> map_chr(1), # 種を最初の単語に短縮 sex = sex |> recode(MALE = "オス", FEMALE = "メス"), # 性別をオス、メスに year = date_egg |> year(), # 観測日から調査年を抽出 bill_length_to_depth_ratio = bill_length / bill_depth # クチバシの長さ÷高さ ) |> select(!date_egg) # 観測日を削除
to be continued... 今後の予定: 1. データセットの紹介(前回) 2. データクリーニング(今回) 3. 探索的データ分析(EDA)と可視化
4. 分析例 ◦ シンプソンのパラドクス ◦ 次元縮約 ◦ クラスタリング ◦ 判別モデル ◦ などなど 次回もお楽しみに!