View source: R/make_clean_names.R
make_clean_names | R Documentation |
Resulting strings are unique and consist only of the _
character, numbers, and letters. By default, the resulting strings will only
consist of ASCII characters, but non-ASCII (e.g. Unicode) may be allowed by
setting ascii=FALSE
. Capitalization preferences can be specified
using the case
parameter.
For use on the names of a data.frame, e.g., in a `%>%`
pipeline,
call the convenience function clean_names
.
When ascii=TRUE
(the default), accented characters are transliterated
to ASCII. For example, an "o" with a German umlaut over it becomes "o", and
the Spanish character "enye" becomes "n".
The order of operations is: make replacements, (optional) ASCII conversion,
remove initial spaces and punctuation, apply base::make.names()
,
apply snakecase::to_any_case
, and add numeric suffixes
to resolve any duplicated names.
This function relies on snakecase::to_any_case
and can take advantage of
its versatility. For instance, an abbreviation like "ID" can have its
capitalization preserved by passing the argument abbreviations = "ID"
.
See the documentation for snakecase::to_any_case
for more about how to use its features.
On some systems, not all transliterators to ASCII are available. If this is
the case on your system, all available transliterators will be used, and a
warning will be issued once per session indicating that results may be
different when run on a different system. That warning can be disabled with
options(janitor_warn_transliterators=FALSE)
.
If the objective of your call to make_clean_names()
is only to translate to
ASCII, try the following instead:
stringi::stri_trans_general(x, id="Any-Latin;Greek-Latin;Latin-ASCII")
.
make_clean_names( string, case = "snake", replace = c(`'` = "", `"` = "", `%` = "_percent_", `#` = "_number_"), ascii = TRUE, use_make_names = TRUE, allow_dupes = FALSE, sep_in = "\\.", transliterations = "Latin-ASCII", parsing_option = 1, numerals = "asis", ... )
string |
A character vector of names to clean. |
case |
The desired target case (default is |
replace |
A named character vector where the name is replaced by the value. |
ascii |
Convert the names to ASCII ( |
use_make_names |
Should |
allow_dupes |
Allow duplicates in the returned names ( |
sep_in |
(short for separator input) if character, is interpreted as a
regular expression (wrapped internally into |
transliterations |
A character vector (if not |
parsing_option |
An integer that will determine the parsing_option.
|
numerals |
A character specifying the alignment of numerals ( |
... |
Arguments passed on to
|
Returns the "cleaned" character vector.
to_any_case()
# cleaning the names of a vector: x <- structure(1:3, names = c("name with space", "TwoWords", "total $ (2009)")) x names(x) <- make_clean_names(names(x)) x # now has cleaned names # if you prefer camelCase variable names: make_clean_names(names(x), "small_camel") # similar to janitor::clean_names(poorly_named_df): # not run: # make_clean_names(names(poorly_named_df))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.