R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
143: Imputation Techniques for Missing Data
You're likely at the point in your project where you've cleaned your strings and handled your dates, but you've hit the "NA Wall." You run a linear model or a correlation matrix, and R just returns NA for everything, or worse, it silently drops 30% of your rows because of na.omit(). Your first instinct is usually to just fill those holes with the average of the column so the code stops crashing. Let's look at why that usually breaks your analysis.
# A typical "quick fix" that causes problems
set.seed(42)
data <- data.frame(
experience = c(1, 2, 3, 15, 18, 20, NA, NA, 22, 1),
salary = c(45000, 48000, 52000, 120000, 130000, 140000, 125000, 135000, 150000, 44000)
)
# The "Rookie" Imputation: Global Mean
data$experience[is.na(data$experience)] <- mean(data$experience, na.rm = TRUE)
print(data)
The Bias of the Global Mean
If you run the code above, you'll notice something jarring. The missing values for experience were likely for the high-earners (since they align with salaries over 120k), but we just filled them with the global average of roughly 11 years. Now, your dataset contains people making 130k with only 11 years of experience, which completely warps the relationship between seniority and pay. You haven't "fixed" the data; you've introduced a systematic bias that will make your regression coefficients lie to you.
Switching to Median for Skewed Distributions
Before jumping to complex algorithms, check your distribution. If your data is skewed (like income or house prices), the mean is a terrible choice because outliers pull it away from the "typical" value. The median is far more robust. I generally lean toward the median for any financial or count data unless I have a specific reason not to.
# A slightly better approach for skewed data
median_val <- median(data$experience, na.rm = TRUE)
data$experience[is.na(data$experience)] <- median_val
This is safer than the mean, but it still treats every missing value the same, regardless of other variables. It's a "blind" fill.
Using MICE for Predictive Imputation
When the relationship between variables is strong, you shouldn't be guessing a single number. You should be predicting the missing value based on the other available data. This is where MICE (Multivariate Imputation by Chained Equations) comes in. Instead of filling a hole with a constant, MICE looks at the rest of the row and says, "Given that this person makes 130k, their experience is likely closer to 17 than to 11."
# You'll need the mice package for this
# install.packages("mice")
library(mice)
# MICE creates multiple imputed datasets to account for uncertainty
# we'll use 'pmm' (predictive mean matching) which is great for most numeric data
imputed_data <- mice(data, m=5, method='pmm', seed=500)
# Complete the data by picking one of the imputed sets
final_df <- complete(imputed_data)
print(final_df)
I prefer MICE because it preserves the variance of the dataset. If you fill 100 rows with the exact same mean value, you've artificially shrunk your standard deviation, which can lead to "significant" p-values that are actually just artifacts of your imputation. MICE keeps the data "natural."
📋 Practical Task
Repairing a Messy Sensor Dataset with MICE
You have been handed a dataset from a weather station where the temperature sensor failed intermittently. You cannot simply drop the rows because the timestamps are critical for the time-series analysis. You also cannot use the global mean because the temperature fluctuates wildly between the "Day" and "Night" cycles.
Your Goal: Use the mice package to impute the missing temp values based on the humidity and pressure columns.
# Setup the broken dataset
weather_data <- data.frame(
temp = c(22.1, 21.5, NA, 15.2, 14.8, NA, 23.0, 22.8, NA, 16.1),
humidity = c(45, 47, 46, 80, 82, 85, 44, 42, 48, 78),
pressure = c(1012, 1011, 1013, 1005, 1004, 1006, 1012, 1011, 1014, 1007)
)
# 1. Load the mice library
# 2. Run the mice() function on weather_data using 'pmm'
# 3. Use complete() to generate the final data frame
# 4. Print the final data frame to verify no NAs remain
There are no comments for now.