Site icon R-bloggers

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

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

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.

< section id="what-is-make.names" class="level1">

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:

< section id="how-to-use-make.names" class="level1">

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)
< section id="making-names-unique-optional" class="level1">

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.

< section id="examples-in-action" class="level1">

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).

< section id="give-it-a-try" class="level1">

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!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version