Lightweight dplyr::if_else with the virtues and vices that come from such an approach. Attempts to replicate dplyr::if_else but written in base R for faster compile time. hutils::if_else should be faster than dplyr::if_else … when it works, but will not work on lists or on factors. Additional attributes may be dropped.

if_else(condition, true, false, missing = NULL)

Arguments

condition

Logical vector.

true, false

Where condition is TRUE/FALSE, use the correspondingtrue/no value. They must have the same typeof as each other and be the same length as condition or length-one.

missing

If condition is NA, use the corresponding na value. Liketrue andfalse, must be of the same type and have the same length as condition, unless it has length one.

Source

Original code but obviously heavily inspired by https://CRAN.R-project.org/package=dplyr.

Value

Where condition is TRUE, the corresponding value in true; where condition is FALSE, the corresponding value in false. Where condition is NA, then the corresponding value in na -- unless na is NULL (the default) in which case the value will be NA (with the same type as true.)

Details

If the result is expected to be a factor then the conditions for type safety are strict and may be made stricter in future.