Alternative to fcase or case_when for 3.4.4

Hello,

I am looking for an alternative to either fcase or case_when as this specific aplication is working on R 3.4.4. I would either like a base way of doing something like this or an older data.table way of doing it.

library(data.table)

x = 1:10
data.table::fcase(
  x < 5L, 1L,
  x > 5L, 3L
)
#>  [1]  1  1  1  1 NA  3  3  3  3  3


library(dplyr)


df <- data.frame(a = c("a", "b", "a"), b = c("b", "a", "a"))

df <- df %>% mutate(
  new = case_when(
    a == "a" & b == "b" ~ "c",
    a == "b" & b == "a" ~ "d",
    TRUE ~ "e")
)

df
#>   a b new
#> 1 a b   c
#> 2 b a   d
#> 3 a a   e

Created on 2022-01-14 by the reprex package (v2.0.1)

Nested ifelse?

df <- df %>% mutate(
  new =  ifelse(a == "a" & b == "b", "c", 
                ifelse( a == "b" & b == "a", "d", "e")))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.