library(conflicted)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
library(arrow)
library(bit64)
## 要求されたパッケージ bit をロード中です
## 
## 次のパッケージを付け加えます: 'bit'
## 
## 以下のオブジェクトは 'package:dplyr' からマスクされています:
## 
##     symdiff
## 
## 以下のオブジェクトは 'package:base' からマスクされています:
## 
##     xor
## 
## Attaching package bit64
## package:bit64 (c) 2011-2017 Jens Oehlschlaegel
## creators: integer64 runif64 seq :
## coercion: as.integer64 as.vector as.logical as.integer as.double as.character as.bitstring
## logical operator: ! & | xor != == < <= >= >
## arithmetic operator: + - * / %/% %% ^
## math: sign abs sqrt log log2 log10
## math: floor ceiling trunc round
## querying: is.integer64 is.vector [is.atomic} [length] format print str
## values: is.na is.nan is.finite is.infinite
## aggregation: any all min max range sum prod
## cumulation: diff cummin cummax cumsum cumprod
## access: length<- [ [<- [[ [[<-
## combine: c rep cbind rbind as.data.frame
## WARNING don't use as subscripts
## WARNING semantics differ from integer
## for more help type ?bit64
# ファイルの読み込み
production             <- read_parquet("https://github.com/ghmagazine/awesomebook_v2/raw/main/data/production.parquet")
production_missing_num <- read_parquet("https://github.com/ghmagazine/awesomebook_v2/raw/main/data/production_missing_num.parquet")
reservation            <- read_parquet("https://github.com/ghmagazine/awesomebook_v2/raw/main/data/reservation.parquet")
customer               <- read_parquet("https://github.com/ghmagazine/awesomebook_v2/raw/main/data/customer.parquet")

10-1 数値型への変換

Q: さまざまな数値型への変換

Awesome

# データフレームを作成
data.frame(v1 = 40000, v2 = 3) |> 
  # さまざまな型への変換
  mutate(
    # 整数型
    v1_int = as.integer(v1),
    v2_int = as.integer(v2),
    # 64bit整数型
    v1_int64 = as.integer64(v1),
    v2_int64 = as.integer64(v2),
    # 倍精度浮動小数点型
    v1_double = as.double(v1),
    v2_double = as.double(v2)
    ) |>
  # 各型同士の計算
  mutate(
    res_int = v1_int / v2_int,
    res_int64 = v1_int64 / v2_int64,
    res_double = v1_double / v2_double
    )
##      v1 v2 v1_int v2_int v1_int64 v2_int64 v1_double v2_double  res_int
## 1 40000  3  40000      3    40000        3     40000         3 13333.33
##   res_int64 res_double
## 1  13333.33   13333.33

10-2 数値の欠損処理

Q: thicknessが欠損しているレコードの削除

Awesome

production_missing_num |>
  drop_na(thickness)
## # A tibble: 892 × 4
##    type  length thickness fault_flg
##    <chr>  <dbl>     <dbl> <lgl>    
##  1 E      274.      40.2  FALSE    
##  2 D       86.3     16.9  FALSE    
##  3 E      124.       1.02 FALSE    
##  4 B      176.      16.4  FALSE    
##  5 B      245.      29.1  FALSE    
##  6 B      226.      39.8  FALSE    
##  7 C      332.      16.8  FALSE    
##  8 A      201.      12.2  FALSE    
##  9 C      276.      29.9  FALSE    
## 10 E      168.       1.27 FALSE    
## # ℹ 882 more rows

Q: 欠損しているthicknessを定数で補完

Awesome

production_missing_num |>
  mutate(thickness = replace_na(thickness, 1))
## # A tibble: 1,000 × 4
##    type  length thickness fault_flg
##    <chr>  <dbl>     <dbl> <lgl>    
##  1 E      274.      40.2  FALSE    
##  2 D       86.3     16.9  FALSE    
##  3 E      124.       1.02 FALSE    
##  4 B      176.      16.4  FALSE    
##  5 B      245.      29.1  FALSE    
##  6 B      226.      39.8  FALSE    
##  7 C      332.      16.8  FALSE    
##  8 A      201.      12.2  FALSE    
##  9 C      276.      29.9  FALSE    
## 10 E      168.       1.27 FALSE    
## # ℹ 990 more rows

Q: 欠損しているthicknessを平均値で補完

Awesome

production_missing_num |>
  mutate(thickness = if_else(is.na(thickness), mean(thickness, na.rm = TRUE), thickness))
## # A tibble: 1,000 × 4
##    type  length thickness fault_flg
##    <chr>  <dbl>     <dbl> <lgl>    
##  1 E      274.      40.2  FALSE    
##  2 D       86.3     16.9  FALSE    
##  3 E      124.       1.02 FALSE    
##  4 B      176.      16.4  FALSE    
##  5 B      245.      29.1  FALSE    
##  6 B      226.      39.8  FALSE    
##  7 C      332.      16.8  FALSE    
##  8 A      201.      12.2  FALSE    
##  9 C      276.      29.9  FALSE    
## 10 E      168.       1.27 FALSE    
## # ℹ 990 more rows

10-3 数値の外れ値除去

Q: thicknessの外れ値を四分位数ベースの外れ値検出で除去

Awesome

# (1) Q1、Q3、IQRの計算
q1 <- quantile(production$thickness, 0.25)
q3 <- quantile(production$thickness, 0.75)
iqr <- q3 - q1

# (2) Q1、Q3、IQRを用いて外れ値を除去
production |>
  dplyr::filter(thickness >= (q1 - 1.5 * iqr) & thickness <= (q3 + 1.5 * iqr))
## # A tibble: 985 × 4
##    type  length thickness fault_flg
##    <chr>  <dbl>     <dbl> <lgl>    
##  1 E      274.      40.2  FALSE    
##  2 D       86.3     16.9  FALSE    
##  3 E      124.       1.02 FALSE    
##  4 B      176.      16.4  FALSE    
##  5 B      245.      29.1  FALSE    
##  6 B      226.      39.8  FALSE    
##  7 C      332.      16.8  FALSE    
##  8 A      201.      12.2  FALSE    
##  9 C      276.      29.9  FALSE    
## 10 E      168.       1.27 FALSE    
## # ℹ 975 more rows

10-4 数値変換

Q: 予約の合計金額の標準化

Awesome

reservation |>
  mutate(total_price = scale(total_price))
## # A tibble: 2,000,000 × 11
##    reservation_id hotel_id customer_id reserved_at         checkin_date       
##             <int>    <int>       <int> <dttm>              <dttm>             
##  1              1     2460       53431 2013-12-31 07:00:14 2014-12-31 00:00:00
##  2              2      962      488390 2013-12-31 08:23:35 2014-12-31 00:00:00
##  3              3      558      341335 2013-12-31 09:02:05 2014-12-31 00:00:00
##  4              4     3666      398981 2013-12-31 23:44:54 2014-12-31 00:00:00
##  5              5     2180      220381 2014-01-01 02:47:50 2014-12-31 00:00:00
##  6              6      974        1494 2014-01-01 07:56:58 2015-01-01 00:00:00
##  7              7     2260       24104 2014-01-01 13:17:06 2014-12-30 00:00:00
##  8              8      314      124883 2014-01-01 14:22:01 2014-12-31 00:00:00
##  9              9     2211       45282 2014-01-01 14:59:04 2014-12-31 00:00:00
## 10             10      333      390595 2014-01-01 20:24:34 2014-12-30 00:00:00
## # ℹ 1,999,990 more rows
## # ℹ 6 more variables: checkout_date <dttm>, length_of_stay <int>,
## #   people_num <int>, total_price <dbl[,1]>, status <chr>, canceled_at <dttm>

Q: 予約の合計金額の対数変換

Awesome

reservation |>
  mutate(total_price = log(total_price))
## # A tibble: 2,000,000 × 11
##    reservation_id hotel_id customer_id reserved_at         checkin_date       
##             <int>    <int>       <int> <dttm>              <dttm>             
##  1              1     2460       53431 2013-12-31 07:00:14 2014-12-31 00:00:00
##  2              2      962      488390 2013-12-31 08:23:35 2014-12-31 00:00:00
##  3              3      558      341335 2013-12-31 09:02:05 2014-12-31 00:00:00
##  4              4     3666      398981 2013-12-31 23:44:54 2014-12-31 00:00:00
##  5              5     2180      220381 2014-01-01 02:47:50 2014-12-31 00:00:00
##  6              6      974        1494 2014-01-01 07:56:58 2015-01-01 00:00:00
##  7              7     2260       24104 2014-01-01 13:17:06 2014-12-30 00:00:00
##  8              8      314      124883 2014-01-01 14:22:01 2014-12-31 00:00:00
##  9              9     2211       45282 2014-01-01 14:59:04 2014-12-31 00:00:00
## 10             10      333      390595 2014-01-01 20:24:34 2014-12-30 00:00:00
## # ℹ 1,999,990 more rows
## # ℹ 6 more variables: checkout_date <dttm>, length_of_stay <int>,
## #   people_num <int>, total_price <dbl>, status <chr>, canceled_at <dttm>

10-5 数値のカテゴリ化

Q: 顧客の年齢のカテゴリ化

Awesome

customer |>
  mutate(age_cat = as.factor(as.integer(age / 10)))
## # A tibble: 500,000 × 9
##    customer_id name       age sex   address_prefecture address_city address_town
##          <int> <chr>    <int> <chr> <chr>              <chr>        <chr>       
##  1           1 山田 裕…    75 <NA>  岐阜県             岐阜市       鷺山清洲町  
##  2           2 藤井 稔     83 M     大阪府             豊能郡能勢町 地黄        
##  3           3 青木 太…    62 M     佐賀県             佐賀市       本庄町袋    
##  4           4 渡辺 裕…    28 M     福島県             喜多方市     豊川町高堂太
##  5           5 渡辺 明…    62 F     兵庫県             西宮市       津門西口町  
##  6           6 西村 知…    66 F     秋田県             仙北郡美郷町 佐野        
##  7           7 斉藤 七…    34 F     高知県             長岡郡大豊町 日浦        
##  8           8 村上 明…    81 F     北海道             夕張市       南部青葉町  
##  9           9 鈴木 直…    57 <NA>  京都府             京都市左京区 上高野石田町
## 10          10 佐藤 舞     59 F     神奈川県           伊勢原市     下糟屋      
## # ℹ 499,990 more rows
## # ℹ 2 more variables: address_zipcode <chr>, age_cat <fct>

以上です。

第11章 カテゴリ

第9章 整形

Top