Write a function in R language to replace the missing value in a vector with the mean of that vector?
Answer Posted / Ankur
Here is a simple function named `fill_mean()` that replaces NA values in a vector with the mean of the non-NA values:
```R
fill_mean <- function(vector) {
if (any(is.na(vector))) {
vector[is.na(vector)] <- mean(vector, na.rm = TRUE)
}
return(vector)
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers