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()).

names0(num, prefix = "x")

dummy_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.

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.

Value

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

Examples

names0(9, "x")
#> [1] "x1" "x2" "x3" "x4" "x5" "x6" "x7" "x8" "x9"
names0(10, "x")
#> [1] "x01" "x02" "x03" "x04" "x05" "x06" "x07" "x08" "x09" "x10"
example <- data.frame(y = ordered(letters[1:5]), z = factor(LETTERS[1:5])) dummy_names("z", levels(example$z)[-1])
#> [1] "z_B" "z_C" "z_D" "z_E"
after_mm <- colnames(model.matrix(~y, data = example))[-1] after_mm
#> [1] "y.L" "y.Q" "y.C" "y^4"
levels(example$y)
#> [1] "a" "b" "c" "d" "e"
dummy_names("y", substring(after_mm, 2), ordinal = TRUE)
#> [1] "y_1" "y_2" "y_3" "y_4"