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)