Skip to content

Add function that creates factor in order of case_when matches #298

@dchiu911

Description

@dchiu911

A common workflow I do is map one vector to another using some (possibly complex) conditions, then coerce to a factor with the level order the same as parsed in dplyr::case_when(). It would be helpful if there was a wrapper that created the factor without having to manually specify the levels.
Currently, I'd do something like this:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

set.seed(2022)
x <- sample(
  c("low", "intermediate", "high"),
  prob = c(0.5, 0.2, 0.3),
  size = 100,
  replace = TRUE
)
z <- rbinom(
  n = 100,
  size = 100,
  prob = 0.3
)
y <- case_when(
  x == "intermediate" | (x == "low" & z < 30) ~ "B",
  x == "low" ~ "A",
  x == "high" ~ "C",
  TRUE ~ NA_character_
) %>%
  factor(levels = c("B", "A", "C"))
str(y)
#>  Factor w/ 3 levels "B","A","C": 1 3 2 3 2 3 2 1 1 3 ...

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

Can we add a function that makes y into a factor with the level order the same as specified in the case_when()? For example,

y <- fct_case(
  x == "intermediate" | (x == "low" & z < 30) ~ "B",
  x == "low" ~ "A",
  x == "high" ~ "C",
  TRUE ~ NA_character_
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions