टेक्स्ट डेटा की सफाई

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-9912 बनाम 617-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 में डेटा क्लीनिंग

और जटिल टेक्स्ट समस्याएँ

  • रेगुलर एक्सप्रेशन अक्षरों का ऐसा क्रम है जो किसी string में मज़बूत सर्च करने देता है।
  • कुछ कैरेक्टर रेगुलर एक्सप्रेशन में अलग तरह से हैंडल होते हैं:
    • (, ), [, ], $, ., +, *, आदि
  • stringr फंक्शन रेगुलर एक्सप्रेशन का उपयोग करते हैं
  • इन कैरेक्टर को खोजने के लिए fixed() चाहिए:
    • str_detect(column, fixed("$"))

 

 

और जानें: String Manipulation with stringr in R और Intermediate Regular Expressions in R

R में डेटा क्लीनिंग

अभ्यास करते हैं!

R में डेटा क्लीनिंग

Preparing Video For Download...