Rename Columns With List in R

How to rename columns based on names from the list in R? From R base functionality, we have colnames() and names() functions that can be used to rename columns from the list. These functions are used to set or get the names of a dataframe object. You can also use setnames() to change old column names with new columns from the list.

In a separate article, I have covered different ways to rename columns in R if you have time, I would recommend reading it.

1. Quick Examples of Rename Columns from List

Following are quick examples to rename 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:


# Output
  id pages   name chapters price
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  

2. Rename Columns From List Using names()

names() is the method available in R which can be used to rename columns from the list (list with column names). You can also use this method to rename dataframe column by index in R.

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


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

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


# Rename column names from list
col_names = list('c1','c2','c3','c4','c5')
names(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

3. Rename Columns From List Using colnames()

colnames() is the method available in R which can be used to rename columns from the list (list with column names). Actually, colnames() is used to set or get the names of a dataframe. You can also use this method to rename dataframe column by index in R.

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


# 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() is a function from data.table library that can be used to select column names from the list. Using this you can also rename multiple selected columns.

data.table is a third-party library hence, in order to use data.table library, you need to first install it by using install.packages('data.table'). Once installation completes, load the data.table library in order to use this setnames() method. To load a library in R use 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, you have learned how to change the dataframe column names from the list of names in R. From R base functionality, we have colnames() and names() functions that can be used to rename columns from the list. These functions are used to set or get the names of a dataframe object.

Related Articles

References

r rename columns list

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Rename Columns With List in R