names0 | R Documentation |
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", call = rlang::caller_env())
dummy_names(var, lvl, ordinal = FALSE, sep = "_")
dummy_extract_names(var, lvl, ordinal = FALSE, sep = "_")
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.
|
var |
A single string for the original factor name. |
lvl |
A character vectors of the factor levels (in order). When used
with |
ordinal |
A logical; was the original factor ordered? |
sep |
A single character value for the separator between the names and levels. |
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.
names0()
returns a character string of length num
and
dummy_names()
generates a character vector the same length as lvl
.
developer_functions
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.