vec_as_names {vctrs} | R Documentation |
Retrieve and repair names
Description
vec_as_names()
takes a character vector of names and repairs it
according to the repair
argument. It is the r-lib and tidyverse
equivalent of base::make.names()
.
vctrs deals with a few levels of name repair:
-
minimal
names exist. Thenames
attribute is notNULL
. The name of an unnamed element is""
and neverNA
. For instance,vec_as_names()
always returns minimal names and data frames created by the tibble package have names that are, at least,minimal
. -
unique
names areminimal
, have no duplicates, and can be used where a variable name is expected. Empty names,...
, and..
followed by a sequence of digits are banned.All columns can be accessed by name via
df[["name"]]
anddf$`name`
andwith(df, `name`)
.
-
universal
names areunique
and syntactic (see Details for more).Names work everywhere, without quoting:
df$name
andwith(df, name)
andlm(name1 ~ name2, data = df)
anddplyr::select(df, name)
all work.
universal
implies unique
, unique
implies minimal
. These
levels are nested.
Usage
vec_as_names(
names,
...,
repair = c("minimal", "unique", "universal", "check_unique", "unique_quiet",
"universal_quiet"),
repair_arg = NULL,
quiet = FALSE,
call = caller_env()
)
Arguments
names |
A character vector. |
... |
These dots are for future extensions and must be empty. |
repair |
Either a string or a function. If a string, it must be one of
The The options |
repair_arg |
If specified and |
quiet |
By default, the user is informed of any renaming
caused by repairing the names. This only concerns unique and
universal repairing. Set Users can silence the name repair messages by setting the
|
call |
The execution environment of a currently
running function, e.g. |
minimal
names
minimal
names exist. The names
attribute is not NULL
. The
name of an unnamed element is ""
and never NA
.
Examples:
Original names of a vector with length 3: NULL minimal names: "" "" "" Original names: "x" NA minimal names: "x" ""
unique
names
unique
names are minimal
, have no duplicates, and can be used
(possibly with backticks) in contexts where a variable is
expected. Empty names, ...
, and ..
followed by a sequence of
digits are banned. If a data frame has unique
names, you can
index it by name, and also access the columns by name. In
particular, df[["name"]]
and df$`name`
and also with(df, `name`)
always work.
There are many ways to make names unique
. We append a suffix of the form
...j
to any name that is ""
or a duplicate, where j
is the position.
We also change ..#
and ...
to ...#
.
Example:
Original names: "" "x" "" "y" "x" "..2" "..." unique names: "...1" "x...2" "...3" "y" "x...5" "...6" "...7"
Pre-existing suffixes of the form ...j
are always stripped, prior
to making names unique
, i.e. reconstructing the suffixes. If this
interacts poorly with your names, you should take control of name
repair.
universal
names
universal
names are unique
and syntactic, meaning they:
Are never empty (inherited from
unique
).Have no duplicates (inherited from
unique
).Are not
...
. Do not have the form..i
, wherei
is a number (inherited fromunique
).Consist of letters, numbers, and the dot
.
or underscore_
characters.Start with a letter or start with the dot
.
not followed by a number.Are not a reserved word, e.g.,
if
orfunction
orTRUE
.
If a vector has universal
names, variable names can be used
"as is" in code. They work well with nonstandard evaluation, e.g.,
df$name
works.
vctrs has a different method of making names syntactic than
base::make.names()
. In general, vctrs prepends one or more dots
.
until the name is syntactic.
Examples:
Original names: "" "x" NA "x" universal names: "...1" "x...2" "...3" "x...4" Original names: "(y)" "_z" ".2fa" "FALSE" universal names: ".y." "._z" "..2fa" ".FALSE"
See Also
rlang::names2()
returns the names of an object, after
making them minimal
.
Examples
# By default, `vec_as_names()` returns minimal names:
vec_as_names(c(NA, NA, "foo"))
# You can make them unique:
vec_as_names(c(NA, NA, "foo"), repair = "unique")
# Universal repairing fixes any non-syntactic name:
vec_as_names(c("_foo", "+"), repair = "universal")