utils_clean_names function

Clean Character Vector of Names

Clean Character Vector of Names

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

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.

Returns

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 ) #suffix and prefix #--------------------------- utils_clean_names( x = x, capitalize_first = TRUE, separator = "_", prefix = "my_prefix", suffix = "my_suffix" )

See Also

Other internal: utils_boxplot_common(), utils_check_args_distantia(), utils_check_args_matrix(), utils_check_args_momentum(), utils_check_args_path(), utils_check_args_tsl(), utils_check_args_zoo(), utils_check_distance_args(), utils_check_list_class(), utils_digits(), utils_distantia_df_split(), utils_prepare_df(), utils_prepare_matrix(), utils_prepare_matrix_list(), utils_prepare_time(), utils_prepare_vector_list(), utils_prepare_zoo_list(), utils_tsl_pairs()

  • Maintainer: Blas M. Benito
  • License: MIT + file LICENSE
  • Last published: 2025-02-01