You are currently viewing Rename Columns With List in R

How to rename columns based on names from the list in R? You can use the R base functions like colnames() and names() functions to rename columns from the list. These functions are particularly useful when you want to update column names without modifying the data itself.

Advertisements

Key Points –

1. Quick Examples of Rename Columns from List

Following are quick examples of renaming columns based on names from the list in R.



# Rename column names from list
col_names = list('c1','c2','c3','c4','c5')
names(my_dataframe) <- c(col_names)

# Rename columns from list
col_names = list('c1','c2','c3','c4','c5')
colnames(my_dataframe) <- c(col_names)

# Using data.table
library(data.table)

# Column names in list
old_names=list('c1','c2','c3','c4','c5')
new_names=list('id','pages','name','chapters','price')

# Rename columns from list
setnames(my_dataframe, old = c(old_names), 
         new = c(new_names))

Let’s create an R DataFrame (data.frame) and run the above examples and validate the results.


# Create dataframe
my_dataframe=data.frame(id=c(11,22,33,44,55),
        pages=c(32,45,33,22,56),
        name=c("spark","python","R","java","jsp"),
        chapters=c(76,86,11,15,7),
        price=c(144,553,321,567,890))

# Display the dataframe
print(my_dataframe)

Output:

r rename columns list

2. Rename Columns From List Using names()


In R, the rename() function is used to replace the names of the columns in a data frame. It allows to replacement of new names for single/multiple columns, either by providing new names directly or by using a mapping between old and new names.

below is the syntax of the names() to use column names from the list.


# Syntax using names()
names(my_dataframe) = c(list_col_names)

Here, we use the c() function to return a one-dimensional vector containing column names from the list. Then, we use names() to assign the names to a dataframe. In the example below, we assign a vector of names to the dataframe column names.


# Rename column names from list
col_names = list('c1','c2','c3','c4','c5')
names(my_dataframe) <- c(col_names)
print("After renaming the columns of data frame:")
print(my_dataframe)

Yields below output.

r rename columns list

3. Rename Columns From List Using colnames()

The colnames() function in R which is commonly used to get or set the column names of a matrix or a data frame. It allows you to retrieve the names of the columns in a dataframe or assign new names to existing ones.

below is the syntax of the colnames() function.


# Syntax using names()
colnames(my_dataframe) = c(list_col_names)

Here c() returns a one-dimensional vector with the column names from the list and colnames() is used to set the names to a dataframe. In the below example, we are assigning a vector of names to dataframe column names. Let’s see an example.


# Rename columns from list
col_names = list('c1','c2','c3','c4','c5')
colnames(my_dataframe) <- c(col_names)
print(my_dataframe)

Yields below output.


# Output
  c1 c2     c3 c4  c5
1 11 32  spark 76 144
2 22 45 python 86 553
3 33 33      R 11 321
4 44 22   java 15 567
5 55 56    jsp  7 890

4. Rename All Selected Columns From List

setnames() function is a part of data.table library that is used to return the names of the column present in the data frame from the list. Using this you can also rename multiple selected columns.

Before going to use the setnames() function you need to install the data.table library using install.packages('data.table'). After completing the installation you can load it using library("data.table").


# Using data.table
library(data.table)

# Column names in list
old_names=list('c1','c2','c3','c4','c5')
new_names=list('id','pages','name','chapters','price')

# Rename columns from list
setnames(my_dataframe, old = c(old_names), 
         new = c(new_names))

#Display column names
print(colnames(my_dataframe))

Yields below output.


# Output column names
[1] "id"       "pages"    "name"     "chapters" "price"   

5. Conclusion

In this article, I have explained how to rename the dataframe column names from the list using R base functions like colnames() and names() functions. Using these functions to replace old column names with new names and also get the names of a dataframe object.

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium