Unlike tidyr::replace_na()
, it is only defined for vectors.
Examples
data <- tibble::tibble(
x = c(TRUE, FALSE, NA),
y = c("x is false", NA, "x is false")
)
dplyr::mutate(data,
x1 = tidyr::replace_na(x, FALSE),
x3 = if_else2(is.na(x) & y == "x is false", FALSE, x),
x4 = replace_na_if(x, y == "x is false", FALSE)
)
#> # A tibble: 3 × 5
#> x y x1 x3 x4
#> <lgl> <chr> <lgl> <lgl> <lgl>
#> 1 TRUE x is false TRUE TRUE TRUE
#> 2 FALSE NA FALSE FALSE FALSE
#> 3 NA x is false FALSE FALSE FALSE