File: names0.Rd

package info (click to toggle)
r-cran-recipes 1.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,636 kB
  • sloc: sh: 37; makefile: 2
file content (70 lines) | stat: -rw-r--r-- 2,261 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
% 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")

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{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 "\verb{^4}", ordinal dummy variables are given simple integer
suffixes such as "\verb{_1}", "\verb{_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)
}