Fast version of base::ifelse()
.
if_else(condition, true, false, missing = NA, ..., ptype = NULL, size = NULL)
Conditions to test on
Values to return if conditions evaluate to TRUE
Values to return if conditions evaluate to FALSE
Value to return if an element of test is NA
These dots are for future extensions and must be empty.
Optional ptype to override output type
Optional size to override output size
x <- 1:5
if_else(x < 3, 1, 0)
#> [1] 1 1 0 0 0
# Can also be used inside of mutate()
df <- data.table(x = x)
df %>%
mutate(new_col = if_else(x < 3, 1, 0))
#> # A tidytable: 5 × 2
#> x new_col
#> <int> <dbl>
#> 1 1 1
#> 2 2 1
#> 3 3 0
#> 4 4 0
#> 5 5 0