make_names

Share:

Description

Make syntactically valid names out from character vectors replacing . (dot) with _ underscore. Hhis is useful when you wish to use snake_case naming convention or are using SQL.

Usage

1
make_names(names, unique = FALSE, leading_ = "")

Arguments

names

character; vector to be coerced to syntactically valid names. This is coerced to character if necessary.

unique

logical; if TRUE, the resulting elements are unique. This may be desired for, e.g., column names.

leading_

What to replace leading '_' and '.' with. Can be only: A-Z, a-z, ., or "" (Defaults)

Calls nake.names and then replaces . by _ See make.names for details.

Multiple consecutive underscores are replaced by a single underscore.

Names the end up with leading underscores are replaced with leading_ which can be a string of any length beginning with a letter or '.'. The default is to drop leading underscores.

This function is idempotent – multiple application of the function do not change the results.

Value

a character vector containing

Author(s)

Christopher Brown

References

https://en.wikipedia.org/wiki/Snake_case
http://titlecase.com

See Also

make.names, make.unique

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
make_names(c("foo and bar", "foo-and-bar"), unique = TRUE)
  # "foo_and_bar"   "foo_and_bar_1"

  make_names(c("foo and bar", "foo.and_bar"), unique = FALSE)
  # "foo.and.bar"  "foo_and_bar"

  make_names(c("foo and bar", "foo.and_bar"), unique = TRUE)
  # "foo_and_bar"   "foo_and_bar_1"

  make_names( c(".foo", "_bar") )  # "foo" "bar"

  make_names( c(".foo", "_bar"), leading="." )  # ".foo" ".bar"