make.names Function in R (3 Examples)

 

In this R post you’ll learn how to make syntactically valid names using the make.names function.

The tutorial will consist of this content:

Let’s dive right in:

 

Example 1: Basic Application of make.names() Function

Example 1 explains how to apply the make.names function to a vector of unique values, i.e. a vector containing five integer values ranging from 1 to 5.

For this, we can use the R syntax below:

make.names(1:5)                         # Apply make.names to unique vector
# [1] "X1" "X2" "X3" "X4" "X5"

As you can see based on the previous output of the RStudio console, the make.names function has returned a character vector containing five valid variable names.

More precisely, the make.names function has added the character “X” as prefix to the input values in our vector.

 

Example 2: Apply make.names() Function to Non-Unique Values

In this section, I’ll show what happens when we apply the make.names function to a vector containing duplicated values.

Have a look at the following R code and its output:

make.names(c(1:5, 1))                   # Apply make.names to non-unique vector
# [1] "X1" "X2" "X3" "X4" "X5" "X1"

As you can see, the first and last elements of our output vector are the same.

Depending on your specific needs, this might lead to problems. Next, I’ll show how to avoid this issue.

 

Example 3: Force make.names() Function to Create Unique Output

The following code demonstrates how to make sure that the make.names command does return unique values only.

For this, we have to specify the unique argument to be equal to TRUE:

make.names(c(1:5, 1), unique = TRUE)    # Set unique argument to TRUE
# [1] "X1"   "X2"   "X3"   "X4"   "X5"   "X1.1"

As you can see, the make.names function has added the suffix .1 to the last element of the output vector.

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which demonstrates the R programming syntax of this article. You can find the video below.

 

The YouTube video will be added soon.

 

In addition, you might read the related tutorials on this website.

 

In this article you have learned how to apply the make.names function in R programming. If you have additional questions, tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top