ft_encode: Encode Factor Levels into Numeric Codes

View source: R/fct_misc.R

ft_encodeR Documentation

Encode Factor Levels into Numeric Codes

Description

Converts the levels of a factor vector into numeric codes, optionally using a provided mapping.

Usage

ft_encode(factor_vec, mapping = NULL)

Arguments

factor_vec

A factor vector to encode.

mapping

An optional named vector providing the numeric code for each level.

Value

A numeric vector with encoded values.

Author(s)

Kai Guo

Examples

# Example factor vector
factor_vec <- factor(c('low', 'medium', 'high', 'medium'))

# Encode without mapping
ft_encode(factor_vec)

# Encode with custom mapping
custom_mapping <- c('low' = 1, 'medium' = 2, 'high' = 3)
ft_encode(factor_vec, mapping = custom_mapping)

fctutils documentation built on Oct. 1, 2024, 1:07 a.m.