データ型の制約

Rでのデータクリーニング

Maggie Matsui

Content Developer @ DataCamp

コース概要

サーバーと虫眼鏡、「汚れたデータを診断」

Rでのデータクリーニング

コース概要

サーバーとバグ、「汚れたデータの副作用」

Rでのデータクリーニング

コース概要

丸いデータベース、ほうきときらめきでクリーンデータ

Rでのデータクリーニング

コース概要

汚れたデータの診断、影響、クリーニング

第1章 — 一般的なデータの問題

Rでのデータクリーニング

なぜデータをきれいにするのか

 

データサイエンスのワークフロー: データ取得、探索と前処理、洞察抽出、レポート

Rでのデータクリーニング

なぜデータをきれいにするのか

 

人的エラーと技術的エラー

Rでのデータクリーニング

なぜデータをきれいにするのか

 

ワークフロー全体に誤りが伝播する

Rでのデータクリーニング

データ型の制約

データ型
文字列 名, 姓, 住所, ...
整数 登録者数, 販売数, ...
小数 気温, 為替レート, ...
二値 既婚か, 新規顧客, はい/いいえ, ...
カテゴリ 婚姻状況, 色, ...
日付 注文日, 生年月日, ...
Rのデータ型
character
integer
numeric
logical
factor
Date
Rでのデータクリーニング

データ型をざっと確認

sales <- read.csv("sales.csv")
head(sales)
  order_id revenue quantity
1     7432   5,454      494
2     7808   5,668      334
3     4893   4,062      259
4     6107   3,936       15
5     7661   1,067      307
6     5908   6,635      235
library(dplyr)
glimpse(sales)
Observations: 100
Variables: 3
$ order_id <dbl> 7432, 7808, ...
$ revenue  <chr> "$5454", "$5668", ...
$ quantity <dbl> 494, 334, ...
Rでのデータクリーニング

データ型の確認

is.numeric(sales$revenue)
FALSE
library(assertive)
assert_is_numeric(sales$revenue)
Error: is_numeric : sales$revenue is not of class 'numeric'; it has class 'character'.
assert_is_numeric(sales$quantity)


Rでのデータクリーニング

データ型の確認

【論理チェック】— TRUE/FALSE を返す

  • is.character()
  • is.numeric()
  • is.logical()
  • is.factor()
  • is.Date()
  • ...

assertive によるチェック】— FALSE ならエラー

  • assert_is_character()
  • assert_is_numeric()
  • assert_is_logical()
  • assert_is_factor()
  • assert_is_date()
  • ...
Rでのデータクリーニング

なぜデータ型が重要か

class(sales$revenue)
"character"
mean(sales$revenue)
NA
Warning message:
In mean.default(sales$revenue) :
  argument is not numeric or logical: returning NA
Rでのデータクリーニング

カンマの問題

sales$revenue
"5,454" "5,668" "4,062" "3,936" "1,067" ...

Rでのデータクリーニング

文字列を数値へ

library(stringr)
revenue_trimmed = str_remove(sales$revenue, ",")

revenue_trimmed
"5454" "5668" "4062" "3936" "1067" ...
as.numeric(revenue_trimmed)
5454 5668 4062 3936 1067 ...
Rでのデータクリーニング

組み合わせて実行

sales %>%
  mutate(revenue_usd = as.numeric(str_remove(revenue, ",")))
# A tibble: 100 x 4
   order_id revenue quantity revenue_usd
      <dbl> <chr>      <dbl>       <dbl>
 1     7432 5,454        494        5454
 2     7808 5,668        334        5668
 3     4893 4,062        259        4062
 4     6107 3,936         15        3936
 5     7661 1,067        307        1067
# ... with 95 more rows
Rでのデータクリーニング

同じ関数でも結果が異なる

mean(sales$revenue)
NA
Warning message:
In mean.default(sales$revenue) :
  argument is not numeric or logical: returning NA
mean(sales$revenue_usd)
5361.4
Rでのデータクリーニング

データ型の変換

  • as.character()
  • as.numeric()
  • as.logical()
  • as.factor()
  • as.Date()
  • ...
Rでのデータクリーニング

注意: factorをnumericへ

product_type
1000 1000 3000 2000 3000
Levels: 1000 2000 3000
class(product_type)
"factor"
as.numeric(product_type)
1 1 3 2 3
as.numeric(as.character(product_type))
1000 1000 3000 2000 3000
Rでのデータクリーニング

Ayo berlatih!

Rでのデータクリーニング

Preparing Video For Download...