make.names | R Documentation |
Make syntactically valid names out of character vectors.
make.names(names, unique = FALSE, allow_ = TRUE)
names |
character vector to be coerced to syntactically valid names. This is coerced to character if necessary. |
unique |
logical; if |
allow_ |
logical. For compatibility with R prior to 1.9.0. |
A syntactically valid name consists of letters, numbers and the dot or
underline characters and starts with a letter or the dot not followed
by a number. Names such as ".2way"
are not valid, and neither
are the reserved words.
The definition of a letter depends on the current locale, but only ASCII digits are considered to be digits.
The character "X"
is prepended if necessary.
All invalid characters are translated to "."
. A missing value
is translated to "NA"
. Names which match R keywords have a dot
appended to them. Duplicated values are altered by
make.unique
.
A character vector of same length as names
with each changed to
a syntactically valid name, in the current locale's encoding.
Some OSes, notably FreeBSD, report extremely incorrect information about which characters are alphabetic in some locales (typically, all multi-byte locales including UTF-8 locales). However, R provides substitutes on Windows, macOS and AIX.
Prior to R version 1.9.0, underscores were not valid in variable names,
and code that relies on them being converted to dots will no longer
work. Use allow_ = FALSE
for back-compatibility.
allow_ = FALSE
is also useful when creating names for export to
applications which do not allow underline in names (for example,
S-PLUS and some DBMSes).
make.unique
,
names
,
character
,
data.frame
.
make.names(c("a and b", "a-and-b"), unique = TRUE) # "a.and.b" "a.and.b.1" make.names(c("a and b", "a_and_b"), unique = TRUE) # "a.and.b" "a_and_b" make.names(c("a and b", "a_and_b"), unique = TRUE, allow_ = FALSE) # "a.and.b" "a.and.b.1" make.names(c("", "X"), unique = TRUE) # "X.1" "X" currently; R up to 3.0.2 gave "X" "X.1" state.name[make.names(state.name) != state.name] # those 10 with a space
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.