This is a wrapper of base::replace() with a simplified interface and a name that target the specific job of filling NA's. See also tidyr::replace_na and dplyr::coalesce.

fill_na(x, filler = 0)

Arguments

x

A dataframe.

filler

A filler; whatever you want to replace NA's with.

Value

A modified version of x.

See also

Other general functions to edit data in place: lookup

Examples

x <- data.frame(x = c(NA, 1), y = c("a", NA), stringsAsFactors = FALSE) a_dataframe <- x fill_na(a_dataframe)
#> x y #> 1 0 a #> 2 1 0
fill_na(a_dataframe, "")
#> x y #> 1 a #> 2 1
fill_na(a_dataframe, "missing")
#> x y #> 1 missing a #> 2 1 missing
a_matrix <- as.matrix(x) fill_na(a_matrix)
#> x y #> [1,] "0" "a" #> [2,] " 1" "0"
a_vector <- x$x fill_na(a_vector)
#> [1] 0 1
a_list <- list(x, x, x) lapply(a_list, fill_na)
#> [[1]] #> x y #> 1 0 a #> 2 1 0 #> #> [[2]] #> x y #> 1 0 a #> 2 1 0 #> #> [[3]] #> x y #> 1 0 a #> 2 1 0 #>