Replaces missing values in data frames, similar to tidyr::replace_na() with a few extras. There are several ways to use this function. If called without any arguments, all missing values will be replaced by the default type. You can change the global default value with the then argument, and you can subset the columns to be affected with the vars argument. Finally, you can specify column-wise replacement values as named arguments, in which case the other arguments are ignored.

# S3 method for data.frame
if_na(.x, ..., then = NULL, otherwise = NULL, vars = NULL)

Arguments

.x

Input vector of values

...

Optional named arguments specifying column-wise replacement values for missing values. Other arguments are ignored by if_na() when used.

then

Global replacement value to be used in place of the default.

otherwise

Ignored.

vars

Optional vector of column names that should receive the missing value replacement. Use the vars() helper to provide bare column names.

Examples

df <- dplyr::tibble( x = c(1, 2, NA), y = c("a", NA, "b"), z = list(1:5, NULL, 10:20) ) df %>% if_na()
#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 "a" <int [5]> #> 2 2 "" <NULL> #> 3 0 "b" <int [11]>
df %>% if_na(then = "0")
#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 a <int [5]> #> 2 2 0 <chr [1]> #> 3 0 b <int [11]>
df %>% if_na(vars = dplyr::vars(x, y))
#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 "a" <int [5]> #> 2 2 "" <NULL> #> 3 0 "b" <int [11]>
df %>% if_na(x = 3, y = "c")
#> # A tibble: 3 x 3 #> x y z #> <dbl> <chr> <list> #> 1 1 a <int [5]> #> 2 2 c <NULL> #> 3 3 b <int [11]>