Back to Practice
#0047
Mean With Missing Values
EasyR10 min10 XP
Problem
Given an R vector with NA values, write the expression that returns the mean of the non-missing values.
Why This Matters
R returns NA by default when missing values are present. Beginners often think the mean function is broken.
Function Signature
mean(x, na.rm = ?)
Examples
Example 1
Inputx <- c(10, 20, NA, 30)
Output20
The missing value is ignored, so the mean is (10 + 20 + 30) / 3.
Constraints
- Use base R.
- Do not manually remove values with indexing for this problem.
CodeR
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
One missing value
Inputc(10, 20, NA, 30)
Expected20
na.rm = TRUE removes NA before calculating the mean.
Hidden Test Categories
No missing valuesMultiple missing valuesDecimal values