본문으로 건너뛰기
cccskills
무료GitHub에 공개

data-cleaning

Prepare survey and administrative datasets for analysis with Python, R, or Stata. Generate cleaning code to handle missing values, duplicates, outliers, and derived variables, with documented transformations and integrity checks that support reproducible research.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

원문원문 설명 보기

Clean and transform messy data for analysis in Python, R, or Stata

설치 방법 보기

이럴 때 유용합니다

  • Clean missing values, duplicates, and outliers in survey data
  • Merge multiple data sources for analysis
  • Prepare panel datasets that track units over time
  • Document data transformations for replication

포함된 파일(1)

  • SKILL.md11.4 KB

SKILL.md(원문)

설치하기 전에 에이전트에게 전달되는 지시 내용을 확인할 수 있습니다.

Data Cleaning

Purpose

This skill helps economists clean, transform, and prepare datasets for analysis in Python, R, or Stata. It emphasizes reproducibility, proper documentation, and handling common data quality issues found in economic research.

When to Use

  • Cleaning raw survey or administrative data
  • Merging multiple data sources
  • Handling missing values, duplicates, and outliers
  • Creating analysis-ready panel datasets
  • Documenting data transformations for replication

Instructions

Step 1: Understand the Data

Before generating code, ask the user:

  1. What is the data source? (survey, administrative, API, etc.)
  2. What is the unit of observation?
  3. What are the key variables needed for analysis?
  4. Are there known data quality issues to address?

Step 2: Generate Cleaning Pipeline

Create a Stata do-file that:

  1. Has a clear header with project info and date
  2. Sets up the environment (clear all, set memory, log)
  3. Loads and inspects raw data
  4. Documents each transformation with comments
  5. Creates a codebook for the final dataset

Step 3: Follow Best Practices

  • Use assert statements to verify data integrity
  • Create labeled variables with label variable
  • Use value labels for categorical variables
  • Generate a log file for reproducibility
  • Save intermediate files when appropriate

Example Output

/*==============================================================================
    Project:    Economic Analysis Data Cleaning
    Author:     [Your Name]
    Date:       [Date]
    Purpose:    Clean raw survey data for regression analysis
    Input:      raw_survey_data.dta
    Output:     cleaned_analysis_data.dta
==============================================================================*/

* ============================================
* 1. SETUP
* ============================================

clear all
set more off
cap log close
log using "logs/data_cleaning_`c(current_date)'.log", replace

* Set working directory
cd "/path/to/project"

* Define globals for paths
global raw_data "data/raw"
global clean_data "data/clean"
global output "output"

* ============================================
* 2. LOAD AND INSPECT RAW DATA
* ============================================

use "${raw_data}/raw_survey_data.dta", clear

* Basic inspection
describe
summarize
codebook, compact

* Check for duplicates
duplicates report id_var
duplicates list id_var if _dup > 0

* ============================================
* 3. VARIABLE CLEANING
* ============================================

* --- Rename variables for clarity ---
rename q1 age
rename q2 income_reported
rename q3 education_level

* --- Clean numeric variables ---
* Replace missing value codes with .
mvdecode age income_reported, mv(-99 -88 -77)

* Cap outliers at 99th percentile
qui sum income_reported, detail
replace income_reported = r(p99) if income_reported > r(p99) & !mi(income_reported)

* --- Clean string variables ---
* Standardize state names
replace state = upper(trim(state))
replace state = "NEW YORK" if inlist(state, "NY", "N.Y.", "N Y")

* --- Create categorical variables ---
gen education_cat = .
replace education_cat = 1 if education_level < 12
replace education_cat = 2 if education_level == 12
replace education_cat = 3 if education_level > 12 & education_level <= 16
replace education_cat = 4 if education_level > 16 & !mi(education_level)

label define edu_lbl 1 "Less than HS" 2 "High School" 3 "College" 4 "Graduate"
label values education_cat edu_lbl

* ============================================
* 4. HANDLE MISSING DATA
* ============================================

* Create missing indicator variables
gen mi_income = mi(income_reported)

* Document missingness
tab mi_income

* Count complete cases
egen complete_case = rownonmiss(age income_reported education_cat)
tab complete_case

* ============================================
* 5. CREATE DERIVED VARIABLES
* ============================================

* Age groups
gen age_group = .
replace age_group = 1 if age >= 18 & age < 30
replace age_group = 2 if age >= 30 & age < 50
replace age_group = 3 if age >= 50 & age < 65
replace age_group = 4 if age >= 65 & !mi(age)

label define age_lbl 1 "18-29" 2 "30-49" 3 "50-64" 4 "65+"
label values age_group age_lbl

* Log income
gen log_income = ln(income_reported + 1)

* ============================================
* 6. DATA VALIDATION
* ============================================

* Assert expected ranges
assert age >= 18 & age <= 120 if !mi(age)
assert income_reported >= 0 if !mi(income_reported)

* Check variable types
assert !mi(id_var)
isid id_var  // Verify unique identifier

* ============================================
* 7. LABEL VARIABLES
* ============================================

label variable age "Age in years"
label variable income_reported "Annual income (USD)"
label variable education_cat "Education category"
label variable log_income "Log of annual income"
label variable mi_income "Missing income indicator"

* ============================================
* 8. FINAL CHECKS AND SAVE
* ============================================

* Keep relevant variables
keep id_var age age_group income_reported log_income ///
     education_cat mi_income state year

* Order variables logically
order id_var year state age age_group income_reported ///
      log_income education_cat mi_income

* Compress to minimize file size
compress

* Save cleaned data
save "${clean_data}/cleaned_analysis_data.dta", replace

* Create codebook
codebook, compact

* Close log
log close

* ============================================
* END OF FILE
* ============================================

Requirements

Software

  • Stata 15+ (some commands require newer versions)

Recommended User-Written Commands

ssc install unique     // For unique value checking
ssc install mdesc      // For missing data patterns
ssc install labutil    // For label manipulation

Best Practices

  1. Always start with clear all to ensure clean environment
  2. Use log files to document all transformations
  3. Comment extensively - explain WHY, not just WHAT
  4. Use assert statements to catch data errors early
  5. Create a data dictionary alongside your cleaned data
  6. Version your do-files and datasets

Common Pitfalls

  • ❌ Not checking for duplicates before merging
  • ❌ Forgetting to handle missing value codes (-99, -88, etc.)
  • ❌ Not labeling variables and values
  • ❌ Overwriting raw data files
  • ❌ Not documenting data transformations

Python Data Cleaning Example

"""
Data Cleaning Pipeline (Python / pandas)
=========================================
Input:  data/raw/raw_data.csv
Output: data/clean/clean_data.parquet
"""

import pandas as pd
import numpy as np
from pathlib import Path

# --------------------------------------------------
# 1. Load and inspect
# --------------------------------------------------
df = pd.read_csv("data/raw/raw_data.csv")
print(df.dtypes)
print(df.describe())
print(df.isnull().sum())  # missingness report

# Check for duplicates
print(f"Duplicate rows: {df.duplicated().sum()}")
df = df.drop_duplicates()

# --------------------------------------------------
# 2. Rename and standardize column names
# --------------------------------------------------
df.columns = (
    df.columns
    .str.strip()
    .str.lower()
    .str.replace(r"\s+", "_", regex=True)
    .str.replace(r"[^a-z0-9_]", "", regex=True)
)

# --------------------------------------------------
# 3. Handle missing value codes
# --------------------------------------------------
MISSING_CODES = [-99, -88, -77, 9999]
df.replace(MISSING_CODES, np.nan, inplace=True)

# --------------------------------------------------
# 4. Fix dtypes
# --------------------------------------------------
df["year"] = pd.to_numeric(df["year"], errors="coerce").astype("Int64")
df["income"] = pd.to_numeric(df["income"], errors="coerce")

# --------------------------------------------------
# 5. Cap outliers at 1st/99th percentile
# --------------------------------------------------
for col in ["income", "wage"]:
    if col in df.columns:
        lo, hi = df[col].quantile([0.01, 0.99])
        df[col] = df[col].clip(lo, hi)

# --------------------------------------------------
# 6. Create derived variables
# --------------------------------------------------
df["log_income"] = np.log1p(df["income"])
df["mi_income"] = df["income"].isna().astype(int)

# --------------------------------------------------
# 7. Validate
# --------------------------------------------------
assert df["id"].notna().all(), "Missing IDs"
assert df["id"].is_unique, "Duplicate IDs"
assert (df["income"].dropna() >= 0).all(), "Negative income"

# --------------------------------------------------
# 8. Save
# --------------------------------------------------
Path("data/clean").mkdir(parents=True, exist_ok=True)
df.to_parquet("data/clean/clean_data.parquet", index=False)
print(f"Saved {len(df):,} rows × {df.shape[1]} columns")

R Data Cleaning Example

# Data Cleaning Pipeline (R / tidyverse)
# Input:  data/raw/raw_data.csv
# Output: data/clean/clean_data.rds

library(tidyverse)
library(janitor)

# --------------------------------------------------
# 1. Load and inspect
# --------------------------------------------------
df <- read_csv("data/raw/raw_data.csv")
glimpse(df)
summary(df)
colSums(is.na(df))  # missingness

# Check duplicates
cat("Duplicate rows:", sum(duplicated(df)), "\n")
df <- distinct(df)

# --------------------------------------------------
# 2. Standardize column names (snake_case)
# --------------------------------------------------
df <- clean_names(df)  # janitor::clean_names

# --------------------------------------------------
# 3. Replace missing value codes
# --------------------------------------------------
MISSING_CODES <- c(-99, -88, -77, 9999)
df <- df %>%
  mutate(across(where(is.numeric), ~ ifelse(. %in% MISSING_CODES, NA, .)))

# --------------------------------------------------
# 4. Cap outliers at 1st/99th percentile
# --------------------------------------------------
winsorize <- function(x, probs = c(0.01, 0.99)) {
  qs <- quantile(x, probs, na.rm = TRUE)
  pmin(pmax(x, qs[1]), qs[2])
}
df <- df %>%
  mutate(across(c(income, wage), winsorize))

# --------------------------------------------------
# 5. Create derived variables
# --------------------------------------------------
df <- df %>%
  mutate(
    log_income = log1p(income),
    mi_income  = as.integer(is.na(income))
  )

# --------------------------------------------------
# 6. Validate
# --------------------------------------------------
stopifnot("Missing IDs"     = !any(is.na(df$id)),
          "Duplicate IDs"   = !any(duplicated(df$id)),
          "Negative income" = all(df$income >= 0, na.rm = TRUE))

# --------------------------------------------------
# 7. Save
# --------------------------------------------------
dir.create("data/clean", recursive = TRUE, showWarnings = FALSE)
saveRDS(df, "data/clean/clean_data.rds")
cat(sprintf("Saved %d rows × %d columns\n", nrow(df), ncol(df)))

Related Skills & Commands

  • data-fetcher: Fetch raw data from economic databases before cleaning
  • stats: Generate summary statistics after cleaning
  • /analyze: Start analysis workflow with your clean dataset
  • table: Format summary tables for publication
  • ols-regression: Proceed to estimation after data preparation

리뷰

아직 리뷰가 없습니다. 사용해 본 소감을 남겨 주세요.

댓글

로그인 없이 쓸 수 있습니다. 사용 후기, 질문, 작동하지 않은 환경 등을 편하게 남겨 주세요.

아직 댓글이 없습니다. 첫 댓글을 남겨 보세요.

보내면 바로 공개됩니다. 이메일 주소나 전화번호 같은 개인 정보는 쓰지 마세요. URL은 2개까지입니다.

0 / 1,000자

이 양식은 reCAPTCHA로 보호되며 Google 개인정보처리방침 및 서비스 약관이 적용됩니다.

같은 저장소의 스킬

a1

무료

Turn a broad research idea into clear, testable questions. Compare familiar and less conventional approaches, structure the selected question around its population and methods, and assess variables, feasibility, and potential theoretical contributions.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Turn a research topic into testable questions
  • Narrow an overly broad research question
  • Compare alternative research angles
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

a2

무료

Helps build a research framework by comparing theories, mapping relationships between variables, and deriving testable hypotheses. Explores less conventional options, examines weaknesses and competing explanations, and produces diagrams with anticipated reviewer objections.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Comparing theories for a research question
  • Deriving hypotheses from relationships between variables
  • Examining weaknesses and anticipated reviewer objections
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

a5

무료

Clarify the philosophical assumptions behind a research project and check whether they fit the proposed methods. Get a reasoned research stance, draft proposal wording, and guidance for explaining how the researcher's background and values shape the study.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Clarifying philosophical foundations for a research proposal
  • Explaining why the chosen research methods fit the study
  • Drafting a statement about the researcher's position
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

Plan and draft academic papers through specialized agent roles covering literature, arguments, citations, and review. Get chapter guidance, revision roadmaps, Traditional Chinese and English abstracts, and documents formatted as LaTeX, DOCX, PDF, or Markdown.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Planning a paper chapter by chapter
  • Drafting a paper from research materials
  • Turning reviewer comments into a revision roadmap
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

Turn a detailed outline into a philosophy or interdisciplinary manuscript. Draft chapters with repeated quality reviews, check terminology and arguments across sections, and prepare the manuscript, evaluation reports, abstract, and metadata for submission.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Draft a full paper from a detailed outline
  • Review argument and citation quality chapter by chapter
  • Check terminology and argument flow across chapters
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

Simulate academic peer review through five reviewer roles tailored to the paper's field. Receive separate critiques, an editorial decision, and a prioritized revision roadmap, or check whether a revised manuscript addresses earlier comments.

한국어 요약은 준비 중입니다. 영어 요약을 표시하고 있습니다.

  • Review a manuscript before journal submission
  • Examine research methods and statistical validity
  • Challenge core arguments and alternative explanations
brycewang-stanford/Auto-Empirical-Research-Skills4.7천2026년 10월 20일 업데이트

brycewang-stanford의 스킬 모두 보기

이 스킬의 문제 신고