replace_na_if {tidyplus}R Documentation

Conditional replacement of NAs with specified values

Description

Unlike tidyr::replace_na(), it is only defined for vectors.

Usage

replace_na_if(x, condition, true)

Arguments

x

Vector with missing values to modify.

condition

Logical vector

true

The replacement values where condition is TRUE.

Details

replace_na_if() is a wrapper on if_else2(is.na(x) & condition, true, x)

Value

A modified version of x that replaces any missing values where condition is TRUE with true.

See Also

tidyr::replace_na() and if_else2()

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))

[Package tidyplus version 0.0.2 Index]