Skip to contents

Clean and format character vectors for use as column names or variable names.

Usage

utils_clean_names(
  x = NULL,
  lowercase = FALSE,
  separator = "_",
  capitalize_first = FALSE,
  capitalize_all = FALSE,
  length = NULL,
  suffix = NULL,
  prefix = NULL
)

Arguments

x

(required, character vector) Names to be cleaned. Default: NULL

lowercase

(optional, logical) If TRUE, all names are coerced to lowercase. Default: FALSE

separator

(optional, character string) Separator when replacing spaces and dots and appending suffix and prefix to the main word. Default: "_".

capitalize_first

(optional, logical) Indicates whether to capitalize the first letter of each name Default: FALSE.

capitalize_all

(optional, logical) Indicates whether to capitalize all letters of each name Default: FALSE.

length

(optional, integer) Minimum length of abbreviated names. Names are abbreviated via abbreviate(). Default: NULL.

suffix

(optional, character string) String to append to the cleaned names. Default: NULL.

prefix

(optional, character string) String to prepend to the cleaned names. Default: NULL.

Value

character vector

Details

The cleanup operations are applied in the following order:

  • Remove leading and trailing whitespaces.

  • Generates syntactically valid names with base::make.names().

  • Replaces dots and spaces with the separator.

  • Coerces names to lowercase.

  • If argument length is provided, base::abbreviate() is used to abbreviate the new column names.

  • If suffix is provided, it is added at the end of the column name using the separator.

  • If prefix is provided, it is added at the beginning of the column name using the separator.

  • If capitalize_first = TRUE, the first letter is capitalized.

  • If capitalize_all = TRUE, all letters are capitalized.

Examples

x <- c(
  "GerMany",
  "spain",
  "SWEDEN"
)

#abbreviate names
#---------------------------
#abbreviate to 4 characters
utils_clean_names(
  x = x,
  capitalize_all = TRUE,
  length = 4
)
#> GerMany   spain  SWEDEN 
#>  "GRMN"  "SPAN"  "SWED" 

#suffix and prefix
#---------------------------
utils_clean_names(
  x = x,
  capitalize_first = TRUE,
  separator = "_",
  prefix = "my_prefix",
  suffix = "my_suffix"
)
#>                       GerMany                         spain 
#> "My_prefix_GerMany_my_suffix"   "My_prefix_spain_my_suffix" 
#>                        SWEDEN 
#>  "My_prefix_SWEDEN_my_suffix"