Learn R Programming

forcats (version 0.0.0.9000)

lvls: Low-level functions for manipulating levels

Description

lvls_reorder leaves values as is, but changes the order. lvls_revalue changes the values of existing levels; there must be one new level for each old level. lvls_extend extends the set of levels; the new level must include the old levels.

Usage

lvls_reorder(f, idx)
lvls_revalue(f, new_levels)
lvls_expand(f, new_levels)

Arguments

f
A factor
idx
A integer index, with one integer for each existing level
new_levels
A character vector of new levels

Details

These functions are less helpful than the higher-level fct_ functions, but are safer than the very low-level manipulation of levels directly, because they are more specific, and hence can more carefully check their arguments.

Examples

Run this code
f <- factor(c("a", "b", "c"))
lvls_reorder(f, 3:1)
lvls_revalue(f, c("apple", "banana", "carrot"))
lvls_expand(f, c("a", "b", "c", "d"))

Run the code above in your browser using DataLab