清理文字資料

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" vs. "(868) 949-4489"
    • "9239 5849 3712 0039" vs. "4490459957881031"
  • 資訊不一致
    • +1 617-167-9912 vs. 617-167-9912
    • "Veronica Hopkins" vs. "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 的資料清理

一起來練習吧!

R 的資料清理

Preparing Video For Download...