Thanks!!! Here I summarize the discussion.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# With dplyr
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
x <- data.frame(x = c(NA, 1), y = c("a", NA), stringsAsFactors = FALSE)
coalesce(x, list(0))
#> x y
#> 1 0 a
#> 2 1 0
coalesce(x, list("missing"))
#> x y
#> 1 missing a
#> 2 1 missing
# With my wrapper
fill_na <- function(x, filler = 0) {
x[is.na(x)] <- filler
x
}
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