テキストデータのクレンジング

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

Maggie Matsui

Content Developer @ DataCamp

テキストデータとは?

データの種類
氏名 "Veronica Hopkins", "Josiah", ...
電話番号 "6171679912", "(868) 949-4489", ...
メール "[email protected]", "[email protected]", ...
パスワード "JZY46TVG8SM", "iamjosiah21", ...
コメント/レビュー "great service!", "This product broke after 2 days", ...
Rでのデータクリーニング

非構造化データの課題

  • 書式の不一致
    • "6171679912""(868) 949-4489"
    • "9239 5849 3712 0039""4490459957881031"
  • 情報の不一致
    • +1 617-167-9912617-167-9912
    • "Veronica Hopkins""Josiah"
  • 無効なデータ
    • 電話番号 "0492" は短すぎる
    • 郵便番号 "19888" は存在しない
Rでのデータクリーニング

顧客データ

customers
# A tibble: 99 x 3
   name            company                     credit_card        
   <chr>           <chr>                       <chr>              
 1 Galena          In Magna Associates         5171 5854 8986 1916
 2 MacKenzie       Iaculis Ltd                 5128-5078-8008-5824
 3 Megan Acosta    Semper LLC                  5502 4529 0732 1744
 4 Phoebe Delacruz Sit Amet Nulla Limited      5419-7308-7424-0944
 5 Jessica         Pellentesque Sed Ltd        5419 2949 5508 9530
# ... with 95 more rows
Rでのデータクリーニング

ハイフン付きカード番号の検出

str_detect(customers$credit_card, "-")
FALSE TRUE FALSE TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE ...
customers %>%
  filter(str_detect(credit_card, "-"))
   name            company                  credit_card        
 1 MacKenzie       Iaculis Ltd              5128-5078-8008-5824
 2 Phoebe Delacruz Sit Amet Nulla Limited   5419-7308-7424-0944
 3 Abel            Lorem PC                 5211-6023-0805-0217 
 ...
Rでのデータクリーニング

ハイフンの置換

customers %>%

mutate(credit_card_spaces = str_replace_all(credit_card, "-", " "))
   name            company                     credit_card_spaces        
 1 Galena          In Magna Associates         5171 5854 8986 1916
 2 MacKenzie       Iaculis Ltd                 5128 5078 8008 5824
 3 Megan Acosta    Semper LLC                  5502 4529 0732 1744
 4 Phoebe Delacruz Sit Amet Nulla Limited      5419 7308 7424 0944
 5 Jessica         Pellentesque Sed Ltd        5419 2949 5508 9530
 ...
Rでのデータクリーニング

ハイフンと空白の削除

credit_card_clean <- customers$credit_card %>%

str_remove_all("-") %>% str_remove_all(" ")
customers %>% mutate(credit_card = credit_card_clean)
   name            company                credit_card     
 1 Galena          In Magna Associates    5171585489861916
 2 MacKenzie       Iaculis Ltd            5128507880085824
 3 Megan Acosta    Semper LLC             5502452907321744
 ...
Rでのデータクリーニング

無効なクレジットカードの特定

str_length(customers$credit_card)
16 16 16 16 16 16 16 16 16 16 16 16 12 16 16 16 16 16 16 16 16 16 16 16 16 ...
customers %>%
  filter(str_length(credit_card) != 16)
  name            company                   credit_card   
1 Jerry Russell   Sed Eu Company            516294099537
2 Ivor Christian  Ut Tincidunt Incorporated 544571330015
3 Francesca Drake Etiam Consulting          517394144089
Rでのデータクリーニング

無効なクレジットカードの除外

customers %>%
  filter(str_length(credit_card) == 16)
   name            company                     credit_card     
 1 Galena          In Magna Associates         5171585489861916
 2 MacKenzie       Iaculis Ltd                 5128507880085824
 3 Megan Acosta    Semper LLC                  5502452907321744
 4 Phoebe Delacruz Sit Amet Nulla Limited      5419730874240944
 5 Jessica         Pellentesque Sed Ltd        5419294955089530
...
Rでのデータクリーニング

より複雑なテキスト処理

  • 「正規表現」は、文字列内を柔軟に検索するための文字の並びです。
  • 正規表現では一部の文字が特別に扱われます:
    • (, ), [, ], $, ., +, * など
  • stringr の関数は正規表現を使用します
  • これらの文字をそのまま検索するには fixed() を使います:
    • str_detect(column, fixed("$"))

 

 

詳しくは String Manipulation with stringr in RIntermediate Regular Expressions in R を参照

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

Ayo berlatih!

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

Preparing Video For Download...