Data cleaning and transformation

असली data गंदा आता है: duplicated() repeat rows पकड़ता है, trimws/tolower text standardise करते हैं, range checks impossible values flag करते हैं, और transformation analysable columns derive करता है: पहले clean कीजिए, फिर compute कीजिए।

10 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

वह average जो confidently ग़लत था

Raw survey पर पहला pass: mean screen time 171 मिनट। Dean के लिए तैयार?

ज़रा क़रीब से देखिए। Roll 117 दो बार दिखता है (एक form दो बार scan हुआ)। एक city " Surat" पढ़ती है leading space के साथ, दूसरी "surat": R तीन Surats को तीन cities गिनता है। और एक screen-time entry कहती है 1620 मिनट: दिन के सत्ताईस घंटे।

आपने जो भी statistic सीखा है वह ख़ुशी से गंदगी का average निकाल देगा। Cleaning वह unglamorous step है जो glamorous steps को सच बनाता है।

Theory

पकाने से पहले धोना

कोई cook बिना धुली सब्ज़ियाँ हांडी में नहीं डालता क्योंकि "recipe skilled हिस्सा है"।

Data cleaning धोना है: doubles हटाइए (पत्थर), labels standardise कीजिए (छिलका), impossible पर सवाल उठाइए (सड़ा हुआ वाला)। Recipe (mean, SD, charts) भरोसे लायक़ तभी है जब धुलाई हुई हो: और professionals पकाने से ज़्यादा वक़्त धोने में लगाते हैं। Checklist में चार stations हैं।

Practical

Four-station cleaning line

survey <- read.csv("survey.csv")

# STATION 1: duplicates
sum(duplicated(survey))            # how many repeat rows? e.g. [1] 1
survey <- survey[!duplicated(survey), ]   # keep first copies only

# STATION 2: inconsistent text
table(survey$city)                 # reveals " Surat", "surat", "Surat"...
survey$city <- trimws(survey$city)  # strip stray spaces
survey$city <- tolower(survey$city) # one consistent case
table(survey$city)                 # confirm: one spelling per city

# STATION 3: impossible values (range check)
survey[survey$screen < 0 | survey$screen > 1440, ]
# roll 121: screen = 1620 (more minutes than a day has!)
survey$screen[survey$screen > 1440] <- NA   # unknown, not zero

# STATION 4: transformation (derived columns, last lesson)
survey$hours <- survey$screen / 60

# save as a NEW versioned file: never overwrite the raw original
write.csv(survey, "survey_clean.csv", row.names = FALSE)

Theory

दो discovery tools

जो आपने देखा ही नहीं उसे fix नहीं कर सकते। दो commands नब्बे percent गंदगी expose करते हैं:

  • table(df$city): हर distinct value अपनी count के साथ: misspellings, stray spaces और case chaos inspection के लिए लाइन में लगते हैं। इसे fix करने से पहले AND बाद में चलाइए: after-table आपका proof है।
  • summary(df): प्रति numeric column min और max: -20 का minimum या 1620 का maximum data entry error चीख़ता है।

आदत: कोई भी statistics आपकी desk से निकलने से पहले, हर categorical column पर table(), हर numeric पर summary()।

Quiz

duplicated(survey) चार rows के लिए FALSE FALSE TRUE FALSE return करता है। TRUE का क्या मतलब है, और survey[!duplicated(survey), ] क्या रखता है?

  1. Row 3 एक पहले की row repeat करता है; ! rows 1, 2 और 4 रखता है: first occurrences बचते हैं, repeats गिरते हैं
  2. Row 3 original है और इसे delete करना चाहिए
  3. Rows 1, 2 और 4 row 3 के duplicates हैं
  4. Command safety के लिए सभी चार rows delete कर देता है
Show the answer

Row 3 एक पहले की row repeat करता है; ! rows 1, 2 और 4 रखता है: first occurrences बचते हैं, repeats गिरते हैं

duplicated() दूसरे appearance से आगे TRUE mark करता है: पहली copy FALSE रहती है, तो ! से negate करना हर चीज़ की exactly एक copy रखता है। Option B convention उलट देता है (original कभी flag नहीं होता): यह distinction मायने रखता है क्योंकि first रखना ही operation को safe बनाता है। इसे fix करने से पहले sum(duplicated(...)) से गिनना आपको problem का size बताता है।

Think first

दो outliers judge कीजिए

दो suspicious screen times range-check debate में बचते हैं: roll 121 with 1620 मिनट, और roll 108 with 600 मिनट। tap करने से पहले: कौन सा correct/NA होता है, कौन सा रहता है, और principle क्या है?

Show the answer

1620 impossible है: एक दिन में 1440 मिनट होते हैं: यह एक data-entry error है (शायद 162 या 620): अगर original form मौजूद है तो fix कीजिए, वरना NA set कीजिए।

600 रहता है: दस घंटे extreme है पर possible (central-tendency lesson वाला binge-watcher असली था)।

Principle: errors clean कीजिए, outliers रखिए। Impossible = remove/repair; improbable-but-real = retain और robust statistics (median!) को इसे handle करने दीजिए। असुविधाजनक real values delete करना cleaning नहीं है: fabrication है।

Watch out

Cleaner की code of conduct

Raw file कभी overwrite मत कीजिए: memory copy clean कीजिए, survey_clean.csv के रूप में save कीजिए: किसी को भी original से आपकी cleaning फिर से run करने में सक्षम होना चाहिए (reproducibility)।

असली outliers कभी delete मत कीजिए क्योंकि वे mean बिगाड़ते हैं: यह median का काम है।

अपने fixes साबित कीजिए: before/after table() जोड़ी वह evidence है जो एक examiner (या auditor) चाहता है।

Theory

असली jobs में घंटे कहाँ जाते हैं

किसी भी data analyst से पूछिए: 60-80% project time cleaning में जाता है, और हर scandalous "study retracted" story एक गंदा dataset छुपाती है। अब आपके पास पूरी line है: discover (table, summary), deduplicate, standardise, range-check, transform, version। अगला lesson आज हमने survey$screen[...] <- NA से बनाई value का सामना करता है: NA असल में क्या है, और statistics holes के आसपास कैसे compute करती है।

Summary

Key takeaways

  • गंदे data पर statistics confidently ग़लत होती है: पहले clean कीजिए, फिर compute कीजिए।
  • duplicated() repeats flag करता है (दूसरी copy से); df[!duplicated(df), ] पहली रखता है।
  • trimws + tolower text standardise करते हैं; before/after table() discovery और proof है।
  • Range checks impossible values पकड़ते हैं (screen > 1440): fix कीजिए या NA set कीजिए, चुपचाप कभी नहीं रखिए।
  • Errors clean कीजिए, असली outliers रखिए: true extremes delete करना fabrication है।
  • Memory copy clean कीजिए; एक NEW versioned file save कीजिए: raw original पवित्र है।
  • Memory hook: पकाने से पहले सब्ज़ियाँ धोइए।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Data Filtering and cleaning

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Data cleaning and transformation · Statistical Methods and Data Analysis (MDC-03) · Gri-Learn