R/variable_calculation.R

Defines functions fill_na_mean

Documented in fill_na_mean

#' Replace missing values with the mean
#'
#' @param numeric.vector A vector with at least one numeric value.
#'
#' @return The input whos NAs are replaced by the input's mean.
#'
#' @export
#'
#' @examples fill_na_mean(c(1, 2, 3, NA, 5, 3, NA, 6))
fill_na_mean <- function(numeric.vector) {
  assertive::assert_is_numeric(numeric.vector) # -> the run-time test
  ifelse(
    is.na(numeric.vector),
    mean(numeric.vector, na.rm = TRUE),
    numeric.vector
  )
}
gontcharovd/WineReviews documentation built on Feb. 29, 2020, 12:42 a.m.