dt_case_when: Case When with data.table

Description Usage Arguments Value Examples

View source: R/case_when.R

Description

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

Usage

1

Arguments

...

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

Value

Vector of the same size as the input vector

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
x <- rnorm(100)
dt_case_when(
  x < median(x) ~ "low",
  x >= median(x) ~ "high",
  is.na(x) ~ "other"
  )

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

tidyfast documentation built on March 20, 2020, 5:08 p.m.