if_else will replace original data with NA when conditions has NAs. Handling only a simple case so it is 20 times faster than case_when (see Examples).

recode_if(cond, yes, no)

Arguments

cond

condition

yes

value when condition is true

no

value when condition is false **or NA**

Examples

x <- c(rnorm(100), NA_real_)
y <- c(rnorm(100), NA_real_)
bench::mark(
    dplyr::case_when(x < 0 ~ 10, T ~ x),
    recode_if(x < 0, 10, x),
    relative = T
)
#> Error in loadNamespace(x): there is no package called ‘bench’
bench::mark(
    dplyr::case_when(x < 0 ~ y, T ~ x),
    recode_if(x < 0, y, x), 
    relative = T
)
#> Error in loadNamespace(x): there is no package called ‘bench’