Skip to contents

Small helper function to collapse factors using a Base R syntax

Usage

collapse_fct_levels(
  x = appdata$ECG$status,
  new_levels = list(old = "old", new = c("new", "updated")),
  warn_non_existent_levels = FALSE
)

Arguments

x

a character string or factor

new_levels

a named list of levels that need to be relabeled. They should be character values. If no names are provided, the first element will be chosen as the factor label.

warn_non_existent_levels

A logical, whether a warning should be given if levels are not existent.

Value

a factor with newly calculated levels

Examples

collapse_fct_levels(
x = LETTERS,
new_levels = list("A-C" = c("A", "B", "C"))
)
#>  [1] A-C A-C A-C D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S  
#> [20] T   U   V   W   X   Y   Z  
#> Levels: A-C D E F G H I J K L M N O P Q R S T U V W X Y Z