recode_if: recode keeping original data when conditions have NA

View source: R/Utils.R

recode_ifR Documentation

recode keeping original data when conditions have NA

Description

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

Usage

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
)
bench::mark(
    dplyr::case_when(x < 0 ~ y, T ~ x),
    recode_if(x < 0, y, x), 
    relative = T
)

kklot/ktools documentation built on Aug. 13, 2024, 7:08 p.m.