Wrangling Names in R: Your Guide to the make.names() Function

code
rtip
operations
Author

Steven P. Sanderson II, MPH

Published

March 11, 2024

Introduction

Ever tried to use a number or special character as a name for a variable or column in R, only to be met with an error? R has specific rules for what constitutes a valid name, and the make.names function is your knight in shining armor when it comes to wrangling these names into something R understands.

What is make.names?

Think of make.names as a name janitor. It takes a vector of characters (potential names) and ensures they comply with R’s naming conventions. These conventions say a valid name:

  • Must start with a letter or a dot (“.”)
  • Can only contain letters, numbers, periods, and underscores
  • Cannot be a reserved word in R (like if, else, or for)

How to Use make.names

Using make.names is straightforward. You simply provide it with a character vector containing your desired names, and it returns a new vector with valid names. Here’s the basic syntax:

new_names <- make.names(old_names)

Making Names Unique (Optional)

By default, make.names doesn’t guarantee unique names. If you have duplicates, it might just keep them. To ensure unique names, add the unique = TRUE argument:

unique_names <- make.names(old_names, unique = TRUE)

This will modify duplicate names slightly to make them distinct.

Examples in Action!

Let’s see make.names in action with some examples:

# Example 1: Fix numeric names
numbers <- c(10, 20, 30)
valid_names <- make.names(numbers)
print(valid_names)
[1] "X10" "X20" "X30"

In this case, make.names prepends an “X” to each number to make them valid names.

# Example 2: Handle special characters
special_chars <- c("data#1", "result$", "graph!")
clean_names <- make.names(special_chars)
print(clean_names)
[1] "data.1"  "result." "graph." 

Here, make.names removes special characters and replaces them with periods (except for “$” which is removed).

Give it a Try!

R is a playground for exploration. Here are some challenges to try with make.names:

  1. Create a vector with names containing spaces and underscores. Use make.names to see how it handles them.
  2. Try using make.names on a data frame’s column names. What happens?
  3. Explore the unique = TRUE argument. Can you think of situations where it might be necessary?

Remember, make.names is your friend when dealing with non-standard names in R. By understanding its purpose and using it effectively, you can keep your R code clean and error-free. Happy coding!