Write R code to replace all missing (NA) values in the following sample dataset with the value 0, then output the value of "data": data <- c(10, NA, 5, NA, 8)
Missing data is common in real-world datasets. R provides functions to detect, remove, or replace missing values.
# Sample data
data <- c(10, NA, 5, NA, 8)
# Check for missing values
is.na(data)
# Remove missing values
clean_data <- na.omit(data)
clean_data
# Replace NA with a specific value
data[is.na(data)] <- 0
data
Proper handling of missing data ensures accurate analysis and prevents errors in calculations.