0
0
R Programmingprogramming~20 mins

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

Choose your learning style9 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.