R: Automatically relabel factor levels, collapse as necessary

fct_relabel {forcats} | R Documentation

## Automatically relabel factor levels, collapse as necessary

### Description

Automatically relabel factor levels, collapse as necessary

### Usage

    fct_relabel(f, fun, ...)

### Arguments

`f` | A factor
`fun` | A function that is applied to each level. Must accept one character argument and return a character vector of the same length as its input.
`...` | Additional arguments to `fun`.

### Examples

    fct_count(gss_cat$rincome)

    convert_income <- function(x) {
      regex <- "^(?:Lt |)[$]([0-9]+).*$"
      is_range <- grepl(regex, x)
      num_income <- as.numeric(gsub(regex, "\\1", x[is_range]))
      num_income <- trunc(num_income / 5000) * 5000
      x[is_range] <- paste0("Gt $", num_income)
      x
    }

    convert_income(levels(gss_cat$rincome))

    rincome2 <- fct_relabel(gss_cat$rincome, convert_income)
    fct_count(rincome2)

[Package forcats version 0.2.0 Index]