Bird
Raised Fist0
R Programmingprogramming~20 mins

Handling missing values (na.rm, na.omit) in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Missing Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of sum() with na.rm parameter
What is the output of the following R code?
R Programming
x <- c(1, 2, NA, 4)
sum(x, na.rm = TRUE)
A7
BNA
CError: missing value
D6
Attempts:
2 left
💡 Hint
The na.rm = TRUE argument tells sum() to ignore missing values.
❓ Predict Output
intermediate
2:00remaining
Effect of na.omit() on a vector
What is the output of this R code?
R Programming
y <- c(3, NA, 5, NA, 7)
na.omit(y)
A3 5 7
B3 NA 5 NA 7
CError: cannot omit NA
DNA NA NA
Attempts:
2 left
💡 Hint
na.omit() removes all NA values from the vector.
❓ Predict Output
advanced
2:00remaining
Sum of vector with NA without na.rm
What happens when you run this R code?
R Programming
z <- c(10, NA, 20)
sum(z)
A30
BError: missing value
C10
DNA
Attempts:
2 left
💡 Hint
sum() returns NA if there is any missing value and na.rm is not TRUE.
🧠 Conceptual
advanced
2:00remaining
Behavior of na.omit() on data frame
Given the data frame below, what will na.omit(df) return?
R Programming
df <- data.frame(a = c(1, NA, 3), b = c(NA, 2, 3))
na.omit(df)
ARows with NA in column 'a' removed, but rows with NA in 'b' kept
BAll rows kept because na.omit only works on vectors
CRows with any NA values removed, so only the last row remains
DError: na.omit cannot be applied to data frames
Attempts:
2 left
💡 Hint
na.omit removes any row with at least one NA in any column.
❓ Predict Output
expert
2:00remaining
Count of non-NA elements using sum and is.na
What is the output of this R code?
R Programming
v <- c(NA, 4, NA, 7, 9)
sum(!is.na(v))
A2
B3
CNA
DError: invalid argument
Attempts:
2 left
💡 Hint
is.na(v) returns TRUE for NA values, !is.na(v) is TRUE for non-NA values, sum counts TRUE as 1.

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

  1. 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.
  2. 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.
  3. Final Answer:

    It tells the function to ignore missing values (NA) during calculation. -> Option A
  4. 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

  1. Step 1: Recall the function name for removing NAs

    The correct function in R to remove missing values is na.omit().
  2. Step 2: Check the syntax usage

    It is used as na.omit(x) where x is the vector or data frame.
  3. Final Answer:

    clean_x <- na.omit(x) -> Option C
  4. Quick Check:

    Use na.omit() to remove NAs [OK]
Hint: Use na.omit() exactly as written to remove missing values [OK]
Common Mistakes:
  • Swapping function name parts like omit.na
  • Using non-existent functions like na.remove
  • Confusing na.omit with na.rm argument
3. What is the output of the following R code?
x <- c(2, NA, 5, NA, 8)
sum(x)
sum(x, na.rm = TRUE)
medium
A. [1] 15 and [1] 15
B. [1] NA and [1] 15
C. [1] 15 and [1] NA
D. [1] NA and [1] NA

Solution

  1. Step 1: Calculate sum without removing NAs

    Calling sum(x) with NA values returns NA because missing values cause the sum to be undefined.
  2. Step 2: Calculate sum with na.rm = TRUE

    Calling sum(x, na.rm = TRUE) ignores NAs and sums only 2 + 5 + 8 = 15.
  3. Final Answer:

    [1] NA and [1] 15 -> Option B
  4. 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

  1. Step 1: Understand why mean returns NA

    By default, mean() returns NA if any missing values exist in the data.
  2. 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.
  3. Step 3: Check options

    Both mean(data, na.rm = TRUE) and mean(na.omit(data)) will give the correct mean ignoring NAs.
  4. Final Answer:

    Both A and C are correct -> Option A
  5. 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

  1. Step 1: Calculate mean ignoring NAs in the column

    Using mean(df$score, na.rm = TRUE) calculates average excluding NAs directly.
  2. Step 2: Remove rows with NA in entire data frame

    na.omit(df) removes all rows with any NA values, including in score.
  3. Step 3: Calculate mean on cleaned data

    Then mean(df_clean$score) calculates mean on data without missing values.
  4. Final Answer:

    mean(df$score, na.rm = TRUE); df_clean <- na.omit(df); mean(df_clean$score) -> Option D
  5. Quick Check:

    Use na.rm=TRUE for mean and na.omit() to clean data frame [OK]
Hint: Use na.rm=TRUE for mean and na.omit() to clean data frame [OK]
Common Mistakes:
  • Using na.omit on vector but not on data frame
  • Forgetting na.rm=TRUE in mean calculation
  • Using na.omit incorrectly on a vector instead of data frame
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard