R/fill_nas.R

Defines functions fill_nas

Documented in fill_nas

#' Fill all NA values in numeric columns with zero
#'
#' Any NA values in a column of type numeric will be replaced with a zero.
#'
#' @param .data a tbl or data frame
#'
#' @return An object of the same class as `.data`
#' @export
#'
#' @examples
#' library(dplyr)
#'
#' data_tbl <- tibble(x = c(1, NA_real_, 3), y = c("a", "b", "C"), z = c(7, NA_real_, NA_real_))
#'
#' fill_nas(data_tbl)
fill_nas <- function(.data) {

  dplyr::mutate_if(
    .tbl       = .data,
    .predicate = is.numeric,
    .funs      = ~ dplyr::if_else(is.na(.), 0, .)
  )

}
RobbyLankford/lankford documentation built on April 24, 2020, 7:37 p.m.