Replace NAs with specified values

replace_na(.x, replace)

Arguments

.x

A data.frame/data.table or a vector

replace

If .x is a data frame, a list() of replacement values for specified columns. If .x is a vector, a single replacement value.

Examples

df <- data.table(
  x = c(1, 2, NA),
  y = c(NA, 1, 2)
)

# Using replace_na() inside mutate()
df %>%
  mutate(x = replace_na(x, 5))
#> # A tidytable: 3 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1    NA
#> 2     2     1
#> 3     5     2

# Using replace_na() on a data frame
df %>%
  replace_na(list(x = 5, y = 0))
#> # A tidytable: 3 × 2
#>       x     y
#>   <dbl> <dbl>
#> 1     1     0
#> 2     2     1
#> 3     5     2