Làm sạch dữ liệu văn bản

Làm sạch dữ liệu trong R

Maggie Matsui

Content Developer @ DataCamp

Dữ liệu văn bản là gì?

Loại dữ liệu Giá trị ví dụ
Tên "Veronica Hopkins", "Josiah", ...
Số điện thoại "6171679912", "(868) 949-4489", ...
Email "[email protected]", "[email protected]", ...
Mật khẩu "JZY46TVG8SM", "iamjosiah21", ...
Bình luận/Đánh giá "great service!", "This product broke after 2 days", ...
Làm sạch dữ liệu trong R

Vấn đề với dữ liệu phi cấu trúc

  • Không nhất quán định dạng
    • "6171679912" so với "(868) 949-4489"
    • "9239 5849 3712 0039" so với "4490459957881031"
  • Không nhất quán thông tin
    • +1 617-167-9912 so với 617-167-9912
    • "Veronica Hopkins" so với "Josiah"
  • Dữ liệu không hợp lệ
    • Số điện thoại "0492" quá ngắn
    • Mã ZIP "19888" không tồn tại
Làm sạch dữ liệu trong R

Dữ liệu khách hàng

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
Làm sạch dữ liệu trong R

Phát hiện số thẻ có dấu gạch ngang

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 
 ...
Làm sạch dữ liệu trong R

Thay dấu gạch ngang

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
 ...
Làm sạch dữ liệu trong R

Xóa dấu gạch ngang và khoảng trắng

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
 ...
Làm sạch dữ liệu trong R

Tìm thẻ tín dụng không hợp lệ

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
Làm sạch dữ liệu trong R

Loại bỏ thẻ tín dụng không hợp lệ

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
...
Làm sạch dữ liệu trong R

Bài toán văn bản phức tạp hơn

  • Một biểu thức chính quy là chuỗi ký tự cho phép tìm kiếm mạnh trong chuỗi.
  • Một số ký tự được xử lý đặc biệt trong biểu thức chính quy:
    • (, ), [, ], $, ., +, *, và các ký tự khác
  • Các hàm stringr dùng biểu thức chính quy
  • Tìm các ký tự này cần dùng fixed():
    • str_detect(column, fixed("$"))

 

 

Tìm hiểu thêm tại String Manipulation with stringr in R & Intermediate Regular Expressions in R

Làm sạch dữ liệu trong R

Ayo berlatih!

Làm sạch dữ liệu trong R

Preparing Video For Download...