Skip to content

Name Variables

Cghlewis edited this page Jan 30, 2023 · 12 revisions

These functions help you name or rename your variables for the following types of situations.

  1. You have data with no column names and if you do not assign variable names, R will add default column names such as X1, X2, X3 and so on.
  2. You have data with nonsensical variable names and need to rename them for clarity.
  3. You may want to rename your variables to make them more descriptive, shorter, remove special characters or spaces, or to concatenate a prefix or suffix to your variables.

The following examples cover common variable naming, renaming, and cleaning functions you can use.

Some variable naming best practices are to make variable names:

  1. Short but descriptive
  2. No spaces between words
  3. No special characters included

Common variable naming styles:

  • snake_case
  • SCREAMING_SNAKE_CASE
  • UpperCamelCase or PascalCase
  • camelCase
  • kebab-case

Name variables

Modify existing variable names


Main functions used in examples

Package Functions
purrr set_names()
dplyr rename(); rename_with()
janitor clean_names()

Other functions used in examples

Package Functions
base names(); paste0(); toupper(); tolower()
tidyselect all_of(); contains()
stringr str_replace(); str_remove(); str_to_upper(); str_c()
tibble deframe()
readxl read_excel()

Resources

Clone this wiki locally