char2factor: Convert a Character Vector Into a Factor

View source: R/char2factor.R

char2factorR Documentation

Convert a Character Vector Into a Factor

Description

I often need a factor with levels the unique values of a character vector (for example: to prevent ggplot2 from sorting the character vector).

Usage

char2factor(x, levels = unique(x))

Arguments

x

A character vector.

levels

The levels to use, see factor.

Value

A factor.

See Also

Other vector functions: escape_non_ascii(), file_string(), powers_of_ten

Examples

x <- c("beech", "oak", "spruce", "fir")
char2factor(x)

fritools documentation built on Feb. 18, 2026, 9:06 a.m.