% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/misc.R
\name{names0}
\alias{names0}
\alias{dummy_names}
\alias{dummy_extract_names}
\title{Naming Tools}
\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{
\item{num}{A single integer for how many elements are created.}

\item{prefix}{A character string that will start each name.}

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

\item{var}{A single string for the original factor name.}

\item{lvl}{A character vectors of the factor levels (in order). When used
with \code{\link[=step_dummy]{step_dummy()}}, \code{lvl} would be the suffixes that result \emph{after}
\code{model.matrix} is called (see the example below).}

\item{ordinal}{A logical; was the original factor ordered?}

\item{sep}{A single character value for the separator between the names and
levels.}
}
\value{
\code{names0()} returns a character string of length \code{num} and
\code{dummy_names()} generates a character vector the same length as \code{lvl}.
}
\description{
\code{names0()} creates a series of \code{num} names with a common prefix. The names
are numbered with leading zeros (e.g. \code{prefix01}-\code{prefix10} instead of
\code{prefix1}-\code{prefix10}). \code{dummy_names} can be used for renaming unordered and
ordered dummy variables (in \code{\link[=step_dummy]{step_dummy()}}).
}
\details{
When using \code{dummy_names()}, factor levels that are not valid variable names
(e.g. "some text  with spaces") will be changed to valid names by
\code{\link[base:make.names]{base::make.names()}}; see example below. This function will also change the
names of ordinal dummy variables. Instead of values such as \code{".L"}, \code{".Q"},
or \code{"^4"}, ordinal dummy variables are given simple integer suffixes such as
\code{"_1"}, \code{"_2"}, etc.
}
\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)
}
\seealso{
\link{developer_functions}
}
