Change Column Names in List of Data Frames in R (Example)

 

In this article you’ll learn how to rename the variable names of data frames in a list in the R programming language.

The tutorial consists of the following contents:

Let’s jump right to the exemplifying R syntax.

 

Constructing Example Data

We’ll use the following two data frames as a basis for this R programming tutorial:

data1 <- data.frame(x1 = 1:5,             # Create first example data frame
                    x2 = 2:6,
                    x3 = 3:7)
data1                                     # Print first example data frame

 

table 1 data frame change column names list data frames r

 

data2 <- data.frame(x1 = letters[1:5],    # Create first example data frame
                    x2 = letters[2:6],
                    x3 = letters[3:7])
data2                                     # Print first example data frame

 

table 2 data frame change column names list data frames r

 

Tables 1 and 2 show the output of the previous R syntax: We have constructed two different data frames.

Let’s save these data frames in a list object:

my_list <- list(data1, data2)             # Join data frames in list
my_list                                   # Print list of data frames
# [[1]]
#   x1 x2 x3
# 1  1  2  3
# 2  2  3  4
# 3  3  4  5
# 4  4  5  6
# 5  5  6  7
# 
# [[2]]
#   x1 x2 x3
# 1  a  b  c
# 2  b  c  d
# 3  c  d  e
# 4  d  e  f
# 5  e  f  g

The previous R code has created a list object containing our two example data frames.

 

Example: Rename Column Names of Data Frames in List Using lapply() & setNames() Functions

The following R programming code demonstrates how to modify the column names of the data frames in a list object.

For this task, we first have to create a vector of new column names. Note that this vector needs to have the same length as the number of columns in our data frames:

new_colnames <- paste0("new", 1:3)        # Specify new column names
new_colnames                              # Print new column names
# [1] "new1" "new2" "new3"

Next, we can use the lapply and setNames functions to change the variable names of all columns in our data frames:

my_list_new <- lapply(my_list,            # Rename column names in list
                      setNames,
                      new_colnames)
my_list_new                               # Print new list
# [[1]]
#   new1 new2 new3
# 1    1    2    3
# 2    2    3    4
# 3    3    4    5
# 4    4    5    6
# 5    5    6    7
# 
# [[2]]
#   new1 new2 new3
# 1    a    b    c
# 2    b    c    d
# 3    c    d    e
# 4    d    e    f
# 5    e    f    g

As you can see, the column names of both our data frames have been set to new1, new2, and new3.

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the examples of this tutorial. You can find the video tutorial below.

 

 

Furthermore, you might want to read the other posts on statisticsglobe.com. Some articles about topics such as lists, merging, and variables are listed below.

 

Summary: You have learned in this article how to change the column names of data frames in a list in the R programming language. Kindly let me know in the comments section below, in case you have any further comments or questions.

 

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