Learn R Programming

fctutils (version 0.0.7)

ft_map_func: Map Factor Levels Using a Function

Description

Transforms factor levels by applying a function that can include complex logic.

Usage

ft_map_func(factor_vec, map_func)

Value

A factor vector with levels mapped according to the function.

Arguments

factor_vec

A factor vector to map.

map_func

A function that takes a character vector of levels and returns a character vector of new levels.

Author

Kai Guo

Examples

Run this code
# Example factor vector
factor_vec <- factor(c('apple', 'banana', 'cherry'))

# Map levels to uppercase if they start with 'a'
ft_map_func(factor_vec, function(x) {
  ifelse(grepl('^a', x), toupper(x), x)
})

Run the code above in your browser using DataLab