names0: Naming Tools

View source: R/misc.R

names0R Documentation

Naming Tools

Description

names0 creates a series of num names with a common prefix. The names are numbered with leading zeros (e.g. prefix01-prefix10 instead of prefix1-prefix10). dummy_names can be used for renaming unordered and ordered dummy variables (in step_dummy()).

Usage

names0(num, prefix = "x", call = rlang::caller_env())

dummy_names(var, lvl, ordinal = FALSE, sep = "_")

dummy_extract_names(var, lvl, ordinal = FALSE, sep = "_")

Arguments

num

A single integer for how many elements are created.

prefix

A character string that will start each name.

call

The execution environment of a currently running function, e.g. caller_env(). The function will be mentioned in error messages as the source of the error. See the call argument of rlang::abort() for more information.

var

A single string for the original factor name.

lvl

A character vectors of the factor levels (in order). When used with step_dummy(), lvl would be the suffixes that result after model.matrix is called (see the example below).

ordinal

A logical; was the original factor ordered?

sep

A single character value for the separator between the names and levels.

Details

When using dummy_names(), factor levels that are not valid variable names (e.g. "some text with spaces") will be changed to valid names by base::make.names(); see example below. This function will also change the names of ordinal dummy variables. Instead of values such as ".L", ".Q", or "⁠^4⁠", ordinal dummy variables are given simple integer suffixes such as "⁠_1⁠", "⁠_2⁠", etc.

Value

names0 returns a character string of length num and dummy_names generates a character vector the same length as lvl.

See Also

developer_functions

Examples

names0(9, "a")
names0(10, "a")

example <- data.frame(
  x = ordered(letters[1:5]),
  y = factor(LETTERS[1:5]),
  z = factor(paste(LETTERS[1:5], 1:5))
)

dummy_names("y", levels(example$y)[-1])
dummy_names("z", levels(example$z)[-1])

after_mm <- colnames(model.matrix(~x, data = example))[-1]
after_mm
levels(example$x)

dummy_names("x", substring(after_mm, 2), ordinal = TRUE)

tidymodels/recipes documentation built on Nov. 29, 2024, 3:05 p.m.