Learn R Programming

fctutils (version 0.0.7)

ft_encode: 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)

Value

A numeric vector with encoded values.

Arguments

factor_vec

A factor vector to encode.

mapping

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

Author

Kai Guo

Examples

Run this code
# 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)

Run the code above in your browser using DataLab