Handling is.na()
In this page:
Handling is.na()
is.na(x) tests each element of a vector for missing values (NA), returning a logical vector. It's essential for cleaning data before analysis, since many functions either propagate NA or need na.rm = TRUE to ignore it. sum(is.na(x)) is a common idiom for counting how many missing values a vector contains.
Warning: Never compare directly with x == NA to test for missing values -- it always returns NA, not TRUE/FALSE. Use is.na(x) instead.
Example: Handling is.na()
values <- c(10, NA, 25, NA, 30)
print(is.na(values))
cat("Number of missing values:", sum(is.na(values)), "\n")
clean_values <- values[!is.na(values)]
print(clean_values)
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: