Handling missing values (na.rm, na.omit)
📖 Scenario: You have collected temperature readings for a week, but some values are missing. You want to calculate the average temperature, ignoring the missing values.
🎯 Goal: Build a small R program that creates a vector with missing values, sets a helper variable to handle missing data, calculates the average temperature ignoring missing values, and prints the result.
📋 What You'll Learn
Create a numeric vector called
temps with exactly these values: 23, NA, 25, 22, NA, 24, 26Create a logical variable called
remove_na and set it to TRUECalculate the average temperature using the
mean() function with the argument na.rm = remove_naPrint the average temperature using
print()💡 Why This Matters
🌍 Real World
Handling missing data is common in real-world data analysis, like weather data, surveys, or sensor readings where some values might be missing.
💼 Career
Data analysts and scientists must know how to handle missing values to get accurate results and avoid errors in reports or models.
Progress0 / 4 steps