📖 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, 26
Create a logical variable called remove_na and set it to TRUE
Calculate the average temperature using the mean() function with the argument na.rm = remove_na
Print 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
1
Create the temperature vector with missing values
Create a numeric vector called temps with these exact values: 23, NA, 25, 22, NA, 24, 26
R Programming
Hint
Use the c() function to create a vector. Use NA to represent missing values.
2
Set the helper variable to remove missing values
Create a logical variable called remove_na and set it to TRUE
R Programming
Hint
Use <- to assign TRUE to remove_na.
3
Calculate the average temperature ignoring missing values
Calculate the average temperature using the mean() function with the argument na.rm = remove_na and store it in a variable called avg_temp
R Programming
Hint
Use mean(temps, na.rm = remove_na) to calculate the average ignoring missing values.
4
Print the average temperature
Print the variable avg_temp using the print() function
R Programming
Hint
Use print(avg_temp) to display the average temperature.
Practice
(1/5)
1. What does the argument na.rm = TRUE do when used inside functions like sum() or mean() in R?
easy
A. It tells the function to ignore missing values (NA) during calculation.
B. It replaces missing values with zero before calculation.
C. It stops the function and returns an error if there are missing values.
D. It counts the number of missing values in the data.
Solution
Step 1: Understand the role of na.rm argument
The na.rm argument stands for "NA remove" and controls whether missing values should be ignored.
Step 2: Effect of setting na.rm = TRUE
When set to TRUE, the function skips over any NA values and calculates the result using only the available data.
Final Answer:
It tells the function to ignore missing values (NA) during calculation. -> Option A
Quick Check:
na.rm = TRUE means ignore NA values [OK]
Hint: Remember: na.rm = TRUE means "remove NA" during calculation [OK]
Common Mistakes:
Thinking na.rm replaces NA with zero
Assuming na.rm causes an error on NA
Confusing na.rm with counting NAs
2. Which of the following is the correct syntax to remove missing values from a vector x using na.omit() in R?
easy
A. clean_x <- na.remove(x)
B. clean_x <- omit.na(x)
C. clean_x <- na.omit(x)
D. clean_x <- remove.na(x)
Solution
Step 1: Recall the function name for removing NAs
The correct function in R to remove missing values is na.omit().
Step 2: Check the syntax usage
It is used as na.omit(x) where x is the vector or data frame.
Final Answer:
clean_x <- na.omit(x) -> Option C
Quick Check:
Use na.omit() to remove NAs [OK]
Hint: Use na.omit() exactly as written to remove missing values [OK]
Calling sum(x) with NA values returns NA because missing values cause the sum to be undefined.
Step 2: Calculate sum with na.rm = TRUE
Calling sum(x, na.rm = TRUE) ignores NAs and sums only 2 + 5 + 8 = 15.
Final Answer:
[1] NA and [1] 15 -> Option B
Quick Check:
sum() returns NA if NA present unless na.rm=TRUE [OK]
Hint: sum() returns NA if NA present; use na.rm=TRUE to ignore [OK]
Common Mistakes:
Assuming sum ignores NA by default
Mixing output order of the two sums
Thinking sum with na.rm=TRUE returns NA
4. You run this code in R:
data <- c(4, NA, 7, NA, 10)
mean(data)
But it returns NA. How can you fix it to get the mean ignoring missing values?
medium
A. Both A and C are correct
B. Use mean(data, na.rm = TRUE)
C. Use mean(na.omit(data))
D. Replace NA with zero manually before mean
Solution
Step 1: Understand why mean returns NA
By default, mean() returns NA if any missing values exist in the data.
Step 2: Ways to ignore missing values
You can either set na.rm = TRUE inside mean() or remove NAs first using na.omit() and then calculate mean.
Step 3: Check options
Both mean(data, na.rm = TRUE) and mean(na.omit(data)) will give the correct mean ignoring NAs.
Final Answer:
Both A and C are correct -> Option A
Quick Check:
Use na.rm=TRUE or na.omit() to ignore NAs in mean [OK]
Hint: Use na.rm=TRUE or na.omit() to ignore missing values [OK]
Common Mistakes:
Forgetting to add na.rm=TRUE
Thinking mean automatically ignores NA
Replacing NA with zero without reason
5. Given a data frame df with numeric column score containing some NAs, which code correctly calculates the average score excluding missing values and also removes rows with NA in score before calculating the mean?
hard
A. mean(df$score, na.rm = FALSE); df_clean <- na.omit(df); mean(df_clean$score)
B. mean(na.omit(df)); df_clean <- na.omit(df); mean(df_clean$score)
C. mean(df$score); df_clean <- na.omit(df$score); mean(df_clean)
D. mean(df$score, na.rm = TRUE); df_clean <- na.omit(df); mean(df_clean$score)
Solution
Step 1: Calculate mean ignoring NAs in the column
Using mean(df$score, na.rm = TRUE) calculates average excluding NAs directly.
Step 2: Remove rows with NA in entire data frame
na.omit(df) removes all rows with any NA values, including in score.
Step 3: Calculate mean on cleaned data
Then mean(df_clean$score) calculates mean on data without missing values.