Does what dplyr::case_when() does, with the same syntax, but with data.table::fifelse() under the hood

dt_case_when(...)

Arguments

...

statements of the form: condition ~ label, where the label is applied if the condition is met

Examples

x <- rnorm(100) dt_case_when( x < median(x) ~ "low", x >= median(x) ~ "high", is.na(x) ~ "other" )
#> [1] "low" "high" "low" "low" "high" "high" "low" "low" "low" "low" #> [11] "low" "high" "high" "low" "high" "low" "low" "low" "high" "low" #> [21] "high" "high" "low" "high" "high" "low" "low" "low" "low" "low" #> [31] "high" "high" "high" "high" "high" "low" "low" "low" "low" "high" #> [41] "low" "low" "low" "low" "high" "high" "low" "high" "high" "low" #> [51] "high" "low" "low" "low" "low" "high" "low" "high" "low" "high" #> [61] "high" "low" "high" "low" "low" "low" "high" "high" "high" "low" #> [71] "low" "high" "high" "high" "high" "high" "high" "low" "high" "low" #> [81] "high" "high" "high" "high" "low" "high" "low" "low" "high" "low" #> [91] "high" "high" "high" "low" "high" "high" "low" "high" "high" "low"
library(data.table) temp <- data.table(pseudo_id = c(1, 2, 3, 4, 5), x = sample(1:5, 5, replace = TRUE)) temp[, y := dt_case_when(pseudo_id == 1 ~ x * 1, pseudo_id == 2 ~ x * 2, pseudo_id == 3 ~ x * 3, pseudo_id == 4 ~ x * 4, pseudo_id == 5 ~ x * 5)]
#> pseudo_id x y #> 1: 1 4 4 #> 2: 2 4 8 #> 3: 3 3 9 #> 4: 4 4 16 #> 5: 5 1 5