names Function in R (2 Examples)

 

This article illustrates how to get or set the names of a data object in the R programming language.

The post consists of two examples for the application of the names function. To be more specific, the content of the article is structured as follows:

Let’s get started:

Definition & Basic R Syntax of names Function

 

Definition: The names R function returns or sets the names of a data object.

 

Basic R Syntax: You can find the basic R programming syntax of the names function below.

names(x)                          # Basic R syntax of names function

 

 

In the following, I’ll show you two examples for the application of the names function in R programming.

Example 1: Assign Names to Vector Using names() Function

In this Example, I’ll explain how to set and get the names of a vector object in R. First, we have to create an example vector:

my_vec <- 1:5                     # Create example vector
my_vec                            # Print example vector
# 1 2 3 4 5

As you can see based on the output of the RStudio console, our example vector (or array) is consisting of five values ranging from 1 to 5. At this point, the vector elements do not have names.

If we want to assign a name to each of the elements of our vector, we can use the following R code:

names(my_vec) <- letters[1:5]     # Assign names to vector
my_vec                            # Print updated vector
# a b c d e 
# 1 2 3 4 5

The previous R syntax assigned the alphabetic letters a, b, c, d, and e as names to our vector elements.

Now, we could also check the names of our vector using the names() function once more:

names(my_vec)                     # Return names of updated vector
# "a" "b" "c" "d" "e"

Looks good!

 

Example 2: Assign Names to List Using names() Function

Example 2 explains how to apply the names command to a list object. Again, we need to create some example data first:

my_list <- list(1:5,              # Create example list
                3,
                "bbb")
my_list                           # Print example list
# [[1]]
# [1] 1 2 3 4 5
# 
# [[2]]
# [1] 3
# 
# [[3]]
# [1] "bbb"

Our list consists of three list elements. None of these list elements has a name yet.

We can use the names function to specify the names of our list elements as shown below:

names(my_list) <- LETTERS[1:3]    # Assign names to list
my_list                           # Print updated list
# $A
# [1] 1 2 3 4 5
# 
# $B
# [1] 3
# 
# $C
# [1] "bbb"

Now, we can check the names of our list elements:

names(my_list)                    # Return names of updated list
# "A" "B" "C"

 

Video & Further Resources

If you need further info on the topics of this tutorial, you might want to watch the following video of my YouTube channel. In the video, I’m explaining the content of this article.

 

 

Furthermore, you could have a look at some of the other articles on my homepage:

 

At this point of the article you should have learned how to use the names function in R programming. Please let me know in the comments section below, if you have additional questions. In addition, please subscribe to my email newsletter for updates on the newest tutorials.

 

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.

Top